zoukankan      html  css  js  c++  java
  • Valid Parentheses

    Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

    The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not.

    Hide Tags
     Stack String
     
    class Solution {
    public:
        bool isValid(string s) {
            stack<char> str;
            for(int i=0;i<s.size();i++)
                if(s[i]==')' || s[i]==']' || s[i]=='}'){
                    if(str.empty())
                        return false;
                    else{
                        char c=str.top();
                        str.pop();
                        if(c=='('&&s[i]!=')' || c=='['&&s[i]!=']' || c=='{'&&s[i]!='}')
                            return false;
                    }
                }else
                    str.push(s[i]);
            return str.empty();
        }
    };
  • 相关阅读:
    二分与三分
    NOIP应试技巧
    数论
    并差集
    最短路
    图的遍历

    最小生成树
    树状数组
    线段树
  • 原文地址:https://www.cnblogs.com/li303491/p/4092739.html
Copyright © 2011-2022 走看看