描述
给出一组字符串,判断是否符合规则
解决
利用stack就解决了。
class Solution {
public:
bool isValid(string s) {
int lenth = s.size();
map<char, char> m;
m.insert(make_pair('{', '}'));
m.insert(make_pair('[', ']'));
m.insert(make_pair('(', ')'));
if (lenth <= 1 || (lenth & 1))
return false;
bool flag = true;
stack<char> sta;
for (int i = 0; i < lenth; ++i){
if (!sta.empty() && m[sta.top()] == s[i])
sta.pop();
else
sta.push(s[i]);
}
return sta.empty();
}
};