leetcode 20. Valid Parentheses

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    if (s===undefined) {
        return false;
    }
    if (s === '') {
        return true;
    }
    
    var stack = [];
    for (var i = 0; i < s.length; i++) {
        //var l = stack.length;
        var c = stack.pop();
        var temp = s[i];
        if (c === undefined) {
            stack.push(temp);
        } else {
            if ((c === '(' && temp === ')')
            || (c === '[' && temp === ']')
            || (c === '{' && temp === '}')) {
                //stack.pop();
            } else {
                stack.push(c);
                stack.push(temp);
            }
        }
    }
    if (stack.length == 0) {
        return true;
    } else {
        return false;
    }
};

你可能感兴趣的:(leetcode 20. Valid Parentheses)