SOURCE

function isValid(s) {
    let stack = [];
    for (let i = 0; i < s.length; i++) {
        if (s[i] === '(' || s[i] === '[' || s[i] === '{') {
            stack.push(s[i]);
            continue;
        }
        if (stack.length === 0) return false;
        let top = stack[stack.length - 1];
        if ((top === '(' && s[i] === ')') ||
            (top === '{' && s[i] === '}') ||
            (top === '[' && s[i] === ']')) {
            stack.pop();
        } else {
            return false;
        }
    }
    return stack.length === 0;
}

// 测试
console.log(isValid("(){}[[]]")); // true
console.log(isValid("([{}])")); // true
console.log(isValid("(}")); // false
console.log(isValid("([)]")); // false
console 命令行工具 X clear

                    
>
console