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.

    用栈会很方便。为了练习vector抽风的没有用栈,很疼……

    class Solution {
    public:
        bool isValid(string s) {
            if(s.length() == 0)return 1;
            
            vector<char> v;
            
            for(int i = 0 ; i < s.length(); i++)
            {
                if(is(s[i]))
                {
                    v.push_back(s[i]);
                }
            }
            if(v.size()%2 ==1)return 0;
            
            while(true)
            {
                int flag = 0;
                for(int i = 0 ; i < v.size()&& v.size() > 0; i++)
                {
                    if(v[i] == ')' || v[i] == ']' || v[i] == '}' )
                    {
                        if(i == 0)return 0;
                        if(v[i] == ')' &&v[i-1] == '('  ||v[i] == ']' &&v[i-1] == '[' || v[i] == '}' &&v[i-1] == '{'   )
                        {
                            v.erase(v.begin() + i -1 , v.begin() + i +1);
                            
                            break;
                        }
                        else
                        return 0;
                    }
                    if(v.size() == 0)return 1;
                    if(i == v.size() - 1)return 0;
                }
                if(v.size() == 0 )break;
            }
            
            return 1;
            
        }
        
        bool is(char ch)
        {
            if(ch == '(' || ch == ')' || ch == '[' || ch ==']' || ch =='{' || ch =='}')
            return 1;
            else
            return 0;
        }
    };
    

      

  • 相关阅读:
    js基础面试篇
    vue自定义指令
    vue兄弟节点通信
    vue----打包上线引用外部cdn
    vue----mockjs
    laravel database opearate1
    laravel seeding
    backtotop组件
    配置节流函数
    failed at the chromedriver@2.33.2 install script
  • 原文地址:https://www.cnblogs.com/pengyu2003/p/3572431.html
Copyright © 2011-2022 走看看