题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
class Solution { public: /** * * @param s string字符串 * @return bool布尔型 */ bool isValid(string s) { // write code here stack<char> stk; char a = s[0]; if (a == '(' || a == '[' || a == '{') stk.push(a); else return false; int i = 1; while (!stk.empty() || (s[i] == '(' || s[i] == '[' || s[i] == '{') ) { char a = s[i]; if (a == '(' || a == '[' || a == '{'){ stk.push(a); i++; } else { char b = s[i]; a = stk.top(); if ((a == '(' && b != ')') || (a == '[' && b != ']') || (a == '{' && b != '}')) return false; else{ stk.pop(); } i++; } } if(i < s.length()) return false; return true; } };