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

原文地址:https://www.cnblogs.com/xingguozhiming/p/10387356.html

时间: 2024-07-31 00:54:09

20. Valid Parentheses(js)的相关文章

20. Valid Parentheses(stack)

Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]"

LeetCode:20. Valid Parentheses(Easy)

1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', '{', '}', '[' 和 ']'. 合法:形如"()[]"."{[()]}" 不合法:形如"([)]"."[[((" 判断所给字符串s是否合法. 3. 解题思路 对字符串s转换成字符数字进行遍历. 利用栈,遇到左半边字符:'

LeetCode解题笔记 - 20. Valid Parentheses

这星期听别人说在做LeetCode,让他分享一题来看看.试了感觉挺有意思,可以培养自己的思路,还能方便的查看优秀的解决方案.准备自己也开始. 解决方案通常有多种多样,我觉得把自己的解决思路记录下来,阶段性查看,一定能对自己有帮助. 这是我做的第一题,所以记录也从这题开始,之后尽力以简短的说明,描述出思路,方便以后能回顾到简介明了的记录. 20. Valid Parentheses Given a string containing just the characters '(', ')', '{

20. Valid Parentheses【leetcode】

20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]&quo

leetcode题解:Valid Parentheses(栈的应用-括号匹配)

题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&

leetCode 20. Valid Parentheses 字符串

20. Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]&quo

【LeetCode-面试算法经典-Java实现】【032-Longest Valid Parentheses(最长有效括号)】

[032-Longest Valid Parentheses(最长有效括号)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid paren

20. Valid Parentheses括号匹配

20 Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]"

[LeetCode]20 Valid Parentheses 有效的括号

[LeetCode]20 Valid Parentheses 有效的括号 Description Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brac