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

  • 相关阅读:
    操作系统(32-45)
    异或运算( ^ )
    计算机网络(16—30)
    操作系统(13-30)
    win7 linux双系统删除linux
    ubuntu安装matplotlib包
    vmware+CentOS 7 无法上网
    Python命令行清屏的简单办法
    jupyter notebook 工作目录修改
    ipython notebook设置工作路径和自动保存.py文件 ipython_notebook_config.py
  • 原文地址:https://www.cnblogs.com/wzhtql/p/10225681.html
Copyright © 2011-2022 走看看