题解 | #有效括号序列#
有效括号序列
https://www.nowcoder.com/practice/37548e94a270412c8b9fb85643c8ccc2
class Solution { public: /** * * @param s string字符串 * @return bool布尔型 */ bool isValid(string s) { // write code here vector<char> mStack; for (int i = 0; i < s.size(); i++) { char c = s[i]; switch (c) { case '(': mStack.push_back(c); break; case '[': mStack.push_back(c); break; case '{': mStack.push_back(c); break; case ')': if(mStack.empty() || mStack.back() !='('){ return false; } mStack.pop_back(); break; case ']': if(mStack.empty() || mStack.back() !='['){ return false; } mStack.pop_back(); break; case '}': if(mStack.empty() || mStack.back() !='{'){ return false; } mStack.pop_back(); break; default: break; } } return mStack.empty(); } };