代码区:
class Solution {
public:
bool isValid(string s) {
int n=s.length();
stack<char> st;
for(char c:s){
if(c=='('||c=='['||c=='{'){
st.push(c);//遇到左括号就压入栈中
}else{
if(st.empty()){
return false;
}
char top=st.top();
st.pop();
if(c=='}'&&top!='{'||c==']'&&top!='['||c==')'&&top!='('){
return false;
}
}
}
return st.empty();
}
};
欢迎各位读者提出意见。
(菜菜奋斗小日记)