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

    给定一个只包括 '('')''{''}''['']' 的字符串,判断字符串是否有效。

    有效字符串需满足:

    1.左括号必须用相同类型的右括号闭合。

    2.左括号必须以正确的顺序闭合。

    注意空字符串可被认为是有效字符串。

    示例 1:

    输入: "()"
    输出: true
    

    示例 2:

    输入: "()[]{}"
    输出: true
    

    示例 3:

    输入: "(]"
    输出: false
    

    示例 4:

    输入: "([)]"
    输出: false
    

    示例 5:

    输入: "{[]}"
    输出: true

    解题答案

    bool isValid(char * s){
        if(s==NULL||s[0]==''){
            return true;
        }
        int length=strlen(s);
        char *stack=(char *)malloc(sizeof(char)*(length));
        int top=0;
        for(int count=0;count<length;count++)
        {
            if(s[count]=='('||s[count]=='{'||s[count]=='['){
                stack[top++]=s[count];
            }
            else{
                if((--top)<0) return false;
                if(s[count]==')'&&stack[top]!='(') return false;
                if(s[count]=='}'&&stack[top]!='{') return false;
                if(s[count]==']'&&stack[top]!='[') return false;
            }
        }
    
        return (!top);
    }
    

      

  • 相关阅读:
    冲刺阶段第三天
    冲刺阶段第二天
    冲刺阶段第一天
    工作项估计
    学习进度条(7-9周)
    团队计划会议01
    第一次冲刺阶段(一)
    软件项目团队报告
    团队项目会议第一次
    团队开发项目需求简介
  • 原文地址:https://www.cnblogs.com/longlyseul/p/12996837.html
Copyright © 2011-2022 走看看