zoukankan      html  css  js  c++  java
  • 20. Valid Parentheses(js)

    20. Valid Parentheses

    Given a string containing just the characters '('')''{''}''[' and ']', determine if the input string is valid.

    An input string is valid if:

    1. Open brackets must be closed by the same type of brackets.
    2. Open brackets must be closed in the correct order.

    Note that an empty string is also considered valid.

    Example 1:

    Input: "()"
    Output: true
    

    Example 2:

    Input: "()[]{}"
    Output: true
    

    Example 3:

    Input: "(]"
    Output: false
    

    Example 4:

    Input: "([)]"
    Output: false
    

    Example 5:

    Input: "{[]}"
    Output: true
    题意:判断多个括号组是否有效
    代码如下:
    /**
     * @param {string} s
     * @return {boolean}
     */
    var isValid = function(s) {
        var arr=[];
        var len=s.length;
        for(var i=0;i<len;i++){
            if(s.charAt(i)=="{"){
                arr.push('}')
            }else if(s.charAt(i)=='['){
                arr.push(']');
            }else if(s.charAt(i)=='('){
                arr.push(')');
            }else{
                if(arr.length==0 || arr.pop() !=s.charAt(i)){
                    return false;
                }
            }
        }
    
        return arr.length===0?true:false;
    };
  • 相关阅读:
    时间复杂度,空间复杂度
    冒泡排序,选择排序,插入排序
    redis集群
    redis进阶
    redis常识--基础
    mysql基本操作
    TCP/IP 的介绍
    OSI七层网络模型&TCP&UDP&三握四挥
    DNS
    局域网&广域网
  • 原文地址:https://www.cnblogs.com/xingguozhiming/p/10387356.html
Copyright © 2011-2022 走看看