1 class Solution { 2 public: 3 bool isValid(string s) { 4 int len = s.size(); 5 if (len%2 == 1) return false; 6 stack<char> list; 7 for (int i = 0; i < len; i++) { 8 if (s[i] == '(' || s[i] == '[' || s[i] == '{') { 9 list.push(s[i]); 10 } else if (!list.empty() && (s[i] == ')' && list.top() == '(' || 11 s[i] == ']' && list.top() == '[' || 12 s[i] == '}' && list.top() == '{')) { 13 list.pop(); 14 } 15 } 16 return list.empty(); 17 } 18 };