题解 | #合法括号序列判断#
合法括号序列判断
https://www.nowcoder.com/practice/d8acfa0619814b2d98f12c071aef20d4
class Parenthesis { public: bool chkParenthesis(string A, int n) { // write code here stack<char> heap; for (int i = 0; i < n; i ++) { if (A[i] == '(') { heap.push(A[i]); } else if (A[i] == ')' && heap.size()) { heap.pop(); } else { return false; } } if (heap.empty()) return true; else return false; } };