题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
class Solution { public: /** * * @param s string字符串 * @return bool布尔型 */ bool isValid(string s) { // write code here stack<char> vaild; int len = s.size(); for(auto x:s){//遍历s //插入的条件 if(vaild.empty()|| (x == '(' ) || (x == '[' ) || (x == '{' ) ){ vaild.push(x); }else{ //弹出的条件 if((vaild.top() == '(' && x == ')') || (vaild.top() == '[' && x == ']') || (vaild.top() == '{' && x == '}') ){ vaild.pop(); }else{ return false; } } } //检测栈底是否为空 if(vaild.empty()) return true; else return false; } };