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