zoukankan      html  css  js  c++  java
  • 有效括号

    class Solution {
    public:
        bool isValid(string s) {
            int flag=1;         //标志位
            int len=s.size();
            if(len==0)
            {
                return true;
            }                           //判断输入是否为空
            char str[len];
            int top=0;          //栈的顶部位置
            for(int j=0;j<len;j++)
            {
                if(s[j]=='(')
                {
                    str[top]='(';           //入栈
                    top++;
                }
                if(s[j]=='[')
                {
                    str[top]='[';               //入栈
                    top++;
                }
                if(s[j]=='{')
                {
                    str[top]='{';               //入栈
                    top++;
                }
                if(s[j]==')')
                {
                    if(top<1)
                    {
                        return false;                 //出栈前要检验
                    }
                    top--;
                    if(str[top]!='(')
                    {
                        flag=0;
                        break;
                    }
                }
                if(s[j]==']')
                {
                    if(top<1)
                    {
                        return false;               //出栈前要检验
                    }
                    top--;      
                    if(str[top]!='[')
                    {
                        flag=0;
                        break;
                    }
                }
                if(s[j]=='}')
                {
                    if(top<1)
                    {
                        return false;                   //出栈前要检验
                    }
                    top--;
                    if(str[top]!='{')
                    {
                        flag=0;
                        break;
                    }
                }
            }
            if(top==0&&flag==1)                 //判断标志位,判断是否在栈底(栈内是否为空)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    };

  • 相关阅读:
    Ubuntu16.04更新记
    「BZOJ2153」设计铁路
    [UVA-11995]I Can Guess the Data Structure!
    [UVA-11100] The Trip
    [UVA-11039]Children's Game
    [BZOJ1008][HNOI2008]越狱
    NOIP2018退役祭
    修马路
    [NOIP2005]过河
    [POJ1958][Strange Tower of Hanoi]
  • 原文地址:https://www.cnblogs.com/wzhtql/p/10225681.html
Copyright © 2011-2022 走看看