20. Valid Parentheses
最後にカッコが残った場合にどうすれば良いのかと思ったら、return emptyで良いのか。空ならtrue、残っていればfalse。
class Solution { public: bool isValid(string s) { stack<char> ch; for(int i = 0; s.length()>i; i++){ if(s[i] == '(' || s[i] == '[' || s[i] == '{'){ ch.push(s[i]); } else if(s[i] == ')'){ if(i==0 || ch.empty() || ch.top()!= '('){ return false; } ch.pop(); } else if(s[i] == ']'){ if(i==0 ||ch.empty() || ch.top()!= '['){ return false; } ch.pop(); } else if(s[i] == '}'){ if(i==0 ||ch.empty() || ch.top()!= '{'){ return false; } ch.pop(); } } return ch.empty(); } };