Skip to content

有效的括号 #13

@JesseZhao1990

Description

@JesseZhao1990

image

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    var arr = [];
    s = s.split('');
    for(var i=0;i<s.length;i++){
        if(s[i] == "(" || s[i] == "[" || s[i] == "{"){
            arr.push(s[i])
        }
        
        if(s[i] == ")"){
            if(arr[arr.length-1] == "("){
                arr.pop();
            }else{
                return false
            }
        }
        if(s[i] == "]"){
            if(arr[arr.length-1] == "["){
                arr.pop();
            }else{
                return false
            }
        }
        if(s[i] == "}"){
            if(arr[arr.length-1] == "{"){
                arr.pop();
            }else{
                return false
            }
        }
    }
    if(arr.length>0){
        return false;
    }
    return true;
    
};

LeetCode原题链接:https://leetcode-cn.com/problems/valid-parentheses/description/

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions