LintCode: Valid Parentheses

C++

stack<char|int|string>, push(), pop(), top(), empty(), size()

 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> sta;
10         for (int i = 0; i < s.size(); i++) {
11             if (s[i] == '(' || s[i] == '{' || s[i] == '[') {
12                 sta.push(s[i]);
13                 continue;
14             }
15             char top = sta.top();
16             sta.pop();
17             if (s[i] == ')' && top != '(') return false;
18             if (s[i] == ']' && top != '[') return false;
19             if (s[i] == '}' && top != '{') return false;
20         }
21         return sta.empty();
22     }
23 };
原文地址:https://www.cnblogs.com/CheeseZH/p/5109518.html