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 };

 

posted on 2015-03-25 07:45  keepshuatishuati  阅读(145)  评论(0编辑  收藏  举报