括号匹配,用STL里的stack可以轻松解决。
class Solution {
public:
bool isValid(string s) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
char match[256];
match[')'] = '(';
match['}'] = '{';
match[']'] = '[';
stack<char> t;
for(int i = 0; i < s.size(); i++){
if(s[i] == '(' || s[i] == '{' || s[i] == '['){
t.push(s[i]);
}
else{
if(t.empty() || t.top() != match[s[i]])
return false;
else t.pop();
}
}
if(t.empty()) return true;
else return false;
}
};