LeetCode力扣 20. 有效的括號 Valid Parentheses 題解代碼 JavaScript

問題 https://leetcode-cn.com/problems/valid-parentheses/

練習使用JavaScript解答

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
    if(s == "")
        return true;
    var arr = [];
    var stu = {
        '}':'{',
        ']':'[',
        ')':'('
    };
    s = String(s);
    for(var i=0;i<s.length;++i) {
        switch(s[i]) {
            case '(':
            case '{':
            case '[':
                arr.push(s[i]);
                break;
            case ')':
            case '}':
            case ']':
                var tmp = arr.pop();
                if(tmp != stu[s[i]])
                    return false;
        }
    }
    return arr.length == 0;
};

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章