20. Valid Parentheses
class Solution {
public:
bool isValid(string s) {
stack<char> tmp;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '{' || s[i] == '(' || s[i] == '[')
tmp.push(s[i]);
else {
if (tmp.empty()) return false; //pay attention !!!
if (s[i] == '}' && tmp.top() != '{') return false;
if (s[i] == ')' && tmp.top() != '(') return false;
if (s[i] == ']' && tmp.top() != '[') return false;
tmp.pop();
}
}
return tmp.empty(); //notice if it's not empty should also return false!!!
}
};