static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();
class Solution {
public:
bool isValid(string s) {
if (s.length() == 0)
return true;
stack<char> st;
st.push(s[0]);
int index = 1;
while(index < s.length()){
if (st.size() > 0 && ((s[index] == ')' && st.top() == '(') || (s[index] == '(' && st.top() == ')') ||
(s[index] == '[' && st.top() == ']') || (s[index] == ']' && st.top() == '[') ||
(s[index] == '{' && st.top() == '}') || (s[index] == '}' && st.top() == '{')
)){
st.pop();
}
else
st.push(s[index]);
index++;
}
if (st.empty())
return true;
else
return false;
}
};
LetCode 20. 有效的括号
最新推荐文章于 2022-05-08 00:02:45 发布