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

  • 相关阅读:
    Python爬虫 | re正则表达式解析html页面
    Python爬虫 | lxml解析html页面
    Python爬虫 | Beautifulsoup解析html页面
    Python爬虫 | requests模拟浏览器发送请求
    http和https协议
    Python爬虫 | 简介
    对于python 作用域新的理解
    这是我的第一篇博客
    No module named HTMLTestRunner
    ImportError: No module named MySQLdb问题的解决
  • 原文地址:https://www.cnblogs.com/wzhtql/p/10225681.html
Copyright © 2011-2022 走看看