版权声明:博客文章都是作者辛苦整理的,转载请注明出处,谢谢! https://cloud.tencent.com/developer/article/1433821
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(char ch : s) {
if(st.empty()) {
st.push(ch);
}
else {
if((ch == ')' && st.top() == '(') || (ch == ']' && st.top() == '[') || (ch == '}' && st.top() == '{')) {
st.pop();
}
else {
st.push(ch);
}
}
}
return st.empty();
}
};
class Solution {
public:
bool isValid(string s) {
stack<char> st;
for(char ch : s) {
if(st.empty()) {
st.push(ch);
}
else {
if((ch == ')' && st.top() != '(') || (ch == ']' && st.top() != '[') || (ch == '}' && st.top() != '{')) {
return false;
}
else if(ch == ')' || ch == ']' || ch == '}'){
st.pop();
}
else {
st.push(ch);
}
}
}
return st.empty();
}
};