题意
- 序列含有'{}', '()', '[]', 判断其是否有效
方法
- stack
代码
bool isValid(string s) {
int N = s.size();
if (N & 1) return false;
stack<char> st;
for (int i = 0; i < N; i++) {
if (s[i] == '(') st.push(')');
else if (s[i] == '{') st.push('}');
else if (s[i] == '[') st.push(']');
else if (st.empty() || st.top() != s[i]) return false;
else st.pop();
}
return st.empty();
}
标签:Parentheses,return,int,else,Valid,push,false,st,LeetCode20
From: https://www.cnblogs.com/Figure_at_a_Window/p/16590040.html