Given a string containing characters '(', ')', '[', ']', '{', '}', a left bracket and right bracket must match, a matched pair of left single brackets can not appear alone Right brackets or parenthesis. Such as: (() []) is valid, [(]) invalid
empty string can be considered effective.
class Solution { public: bool isValid(string s) { int len = s.length(); stack<char> Tmp; for(int i=0; i<len; i++) { if(s[i] == '(' || s[i] == '{' || s[i] == '[') { Tmp.push(s[i]); } else if(s[i] == ')' || s[i] == '}' || s[i] == ']') { if(Tmp.empty()) return false; if(s[i] == ')') { if(Tmp.top() != '(') { return false; } Tmp.pop(); } else if(s[i] == '}') { if(Tmp.top() != '{') { return false; } Tmp.pop(); } else if(s[i] == ']') { if(Tmp.top() != '[') { return false; } Tmp.pop(); } } } if(!Tmp.empty()) { return false; } return true; } };
Number may be concerned about the public learn more about interview skills