有效的括号

问题描述

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

有效字符串需满足:

  • 左括号必须用相同类型的右括号闭合。
  • 左括号必须以正确的顺序闭合。

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

TIP

输入: "()"

输出: true

TIP

输入: "()[]{}"

输出: true

TIP

输入: "(]"

输出: false

TIP

输入: "([)]"

输出: false

TIP

输入: "{[]}"

输出: true

解题思路

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    if(!s.length) return true;
    let arr = s.split(''),
        res = [],
        left = ['(', '[', '{'],
        right = [')', ']', '}'];
    arr.map((element, index, array) => {
        if(index === 0){
            res.push(element);
        }else{
            let leftIndex = left.indexOf(res[res.length - 1]),
                rightIndex = right.indexOf(element);
            if(leftIndex > -1 && rightIndex > -1 && leftIndex === rightIndex){
                res.pop();
            }else{
                res.push(element);
            }
        }
    });
    return res.length ? false : true;
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
上次更新: 2019-2-26 19:09:35