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;
            }
        }
    };

  • 相关阅读:
    滑动拼图
    CentOS8安装中文输入法
    windows+ubuntu 双系统时间不一致的问题
    Goland 2019下载和安装(带破解补丁和汉化包)
    防火墙站名资源脚本
    linux上以服务方式启动程序kestrel
    NLog实践记录
    sqlserver安装ubuntu
    pyspark提交集群任务
    无法打开hadoop的UI页面
  • 原文地址:https://www.cnblogs.com/wzhtql/p/10225681.html
Copyright © 2011-2022 走看看