• 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 };
  • 相关阅读:
    反爬的几种手段总结
    算法基础篇一
    python总结九
    python总结八
    python总结七
    python总结六
    初识Machine学习
    python总结五
    python总结四
    python总结三
  • 原文地址:https://www.cnblogs.com/CheeseZH/p/5109518.html
Copyright © 2020-2023  润新知