[LintCode] 有效的括号序列

 1 class Solution {
 2 public:
 3     /**
 4      * @param s A string
 5      * @return whether the string is a valid parentheses
 6      */
 7     bool isValidParentheses(string& s) {
 8         // Write your code here
 9         stack<char> parens;
10         for (int i = 0; i < (int)s.length(); i++) {
11             if (s[i] == '(' || s[i] == '[' || s[i] == '{')
12                 parens.push(s[i]);
13             else if (parens.empty() || !match(s[i], parens.top()))
14                 return false;
15             else parens.pop();
16         }
17         return parens.empty();
18     }
19 private:
20     bool match(char s, char t) {
21         if (s == ')') return t == '(';
22         if (s == ']') return t == '[';
23         if (s == '}') return t == '{';
24     }
25 };
原文地址:https://www.cnblogs.com/jcliBlogger/p/4605645.html