code-栈 字符串

leetcode有效的括号

利用栈来匹配字符串

class Solution {
public:
bool isValid(string s) {
if(s.length()%2!=0)
return false;
else
{
stack<char> s1;
map<char,char> m1;
map<char,char>::iterator it;
m1[')']='(';
m1[']']='[';
m1['}']='{';
for(int i=0;i<s.length();i++)
{
it=m1.find(s[i]);
if(it!=m1.end())
{
if(!s1.empty()&&it->second==s1.top())
s1.pop();
else
return false;
}
else
s1.push(s[i]);
}
return s1.empty();
}
}
};

posted @ 2020-05-04 21:38  我爱微风  阅读(104)  评论(0)    收藏  举报