why my JS code is wrong?


  • 0
    S

    the RunCode result seems ok, but code cannot be accepted.
    leetcode's problem or mine?

    var left = ['(','[','{'];
    var right = [')',']','}'];
    var arr = [];
    
    var isValid = function(s) {
        var strArr = s.split('');
        for(var i=0;i<strArr.length;i++){
            if(left.indexOf(strArr[i])>-1){
                arr.push(strArr[i]);
            }else if(right.indexOf(strArr[i])>-1){
                if(left.indexOf(arr.pop())!=right.indexOf(strArr[i])){
                    return false;
                }
            }
        }
        return !arr.length
    };
    
    

  • 0
    S

    btw, the went wrong input is "]". Leetcode says the output is true.


  • 0

    @seeluck Try move var arr = []; inside the function declaration.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.