20有效的括号
题目链接:https://leetcode-cn.com/problems/valid-parentheses/
这题没啥难的,用栈一下子就完事了,不过第一次写出测试时间为0ms的代码,还是挺有趣的。
class Solution {
public:
bool isValid(string s) {
bool result=true;
stack<char> tem;
unordered_map<char,char> mymap={{')','('},{']','['},{'}','{'}};
unordered_set<char> myset={'(','[','{'};
int i=0;
for(i=0;i<s.size();++i){
if(myset.find(s[i])!=myset.end())
tem.push(s[i]);
else{
if(tem.empty()!=true&&tem.top()==mymap[s[i]])
tem.pop();
else{
result=false;
break;
}
}
}
if(tem.empty()!=true)
result=false;
return result;
}
};