题目链接:https://leetcode-cn.com/problems/valid-parentheses/
题目如下:
class Solution {
public:
bool isValid(string s) {
stack<char> stack;
unordered_map<char,char> hashmap{
{'(',')'},{'[',']'},{'{','}'}
};
for(auto e:s)
{
char right=e;
if(e=='('||e=='['||e=='{'){//左括号
stack.push(e);
}
else if(stack.empty()==false){//右括号
char left=stack.top();
if(hashmap[left]==right){
stack.pop();
}
else{
return false;
}
}else{
return false;
}
}
return stack.empty();
/*stack<char> stk;//大宝的
unordered_map<char,char> unmap{
{'(',')'},{'[',']'},{'{','}'}
};
for (auto e : s) {
if (e == '[' || e == '(' || e == '{') {
stk.push(unmap[e]);
}
else if (e == ']' || e == ')' || e == '}') {
if (stk.size() && stk.top() == e) {
stk.pop();
}
else {
return false;
}
}
}
return stk.empty();*/
}
};
tip: