class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(auto t:s){
if(t=='('||t=='['||t=='{')st.push(t);
else if((t==')'||t==']'||t=='}')&&st.empty())return false;//小心这种情况
else if(t==')'&&st.top()=='(')st.pop();
else if(t==']'&&st.top()=='[')st.pop();
else if(t=='}'&&st.top()=='{')st.pop();
else return false;
}
return st.empty();
}
};
Leetcode13 - 20. Valid parentheses
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=324208267&siteId=291194637
Recommended
Ranking