Valid Parentheses easy题
干
写了这题我才发现我不会用stack
以前作业全都用vector+双循环写
我好可悲
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(int i =0; i<s.length(); i++){
char c = s[i];
if(c == '(' or c == '[' or c == '{'){
st.push(c);
}else{
if(st.empty()){
return false;
}
if(c == ')' and st.top() == '(' or
c == ']' and st.top() == '[' or
c == '}' and st.top() == '{' ){
st.pop();
}else{
return false;
}
}
}
if(st.empty()){
return true;
}
return false;
}
};