Skip to content

Latest commit

 

History

History
29 lines (24 loc) · 559 Bytes

20-有效的括号.md

File metadata and controls

29 lines (24 loc) · 559 Bytes

有效的括号

原题

解法

const isValid = function(s) {
    let stack = [];
    let obj = {
        '[': ']',
        '{': '}',
        '(': ')'
    };
    
    for (let i = 0; i < s.length; i++) {
        if (s[i] === "[" || s[i] === '{' || s[i] === '(') {
            stack.push(s[i]);
        } else {
            let key = stack.pop();
            if (obj[key] !== s[i]) {
                return false;
            }
        }
    }
    
    return stack.length === 0;
}