20栈 符号匹配
1.首先要是偶数
2.c++使用哈希表
unordered_map<int,int> m; //<string,string>,<char,char>
C++中使用哈希表(unordered_map)的一些常用操作方法_C 语言_脚本之家
class Solution {
public:
bool isValid(string s) {
if(s.size() % 2 == 1){
return false;
}
stack<char> stk;
unordered_map<char,char> map = {
{')','('},
{']','['},
{'}','{'}
};
for(char a : s){
// 当前字符是否存在于map中
if(map.count(a)){
//当栈为空 或 栈顶不等于 ch在map中对应的value
if(stk.empty() || stk.top() != map[a]){
return false;
}
stk.pop();
}
else{
stk.push(a);//左括号入栈
}
}
return stk.empty();
}
};