20. 有效的括号
思路:1.用栈,2.用left代表有多少个左边的符号
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(char ch: s){
if(st.size()==0) st.push(ch);
else{
char top = st.top();
if(ch == ')' && top=='(') st.pop();
else if(ch == ']' && top=='[') st.pop();
else if(ch == '}' && top=='{') st.pop();
else st.push(ch);
}
}
return st.empty();
}
};