class Solution {
public:
bool isValid(string s) {
int n = s.size();
if (n%2==1){
return false;
}
unordered_map<char, char> mymap={{')', '('}, {'}', '{'}, {']', '['}};
stack<char> st;
for (char ch:s){
if (mymap.count(ch)){
if (st.empty() || st.top() != mymap[ch]){
return false;
}
st.pop();
}
else{
st.push(ch);
}
}
return st.empty();
}
};
Leetcode20. 有效的括号
最新推荐文章于 2024-04-29 10:24:20 发布