Submission Result: Wrong Answer with Javascript


  • 0
    W

    Following is my code in Javascript. I fail in one test case.

     Input: 010", Output: 0, Expected: 10
    
    /**
     * @param {string} str
     * @return {number}
     */
    var myAtoi = function(str) {
        if(str === null){return 0;}//empty string
        
        //ignore prefix whitespace
        var i = 0;
        while(str[i] != '\0' && str[i] === '')
        { 
            ++i;
           
        }
        
        var max = 2147483647, min = -2147483648, signal = 1;
        
        //deal with '+' and '-'
        if(str[i] == '+'){signal = 1; ++i;}
        else if(str[i] == '-'){signal = -1; ++i;}
        
        //string to integer
        var total = 0;
        while(str[i] != '\0'){
            if(str[i] >= '0' && str[i] <= '9'){
                total = total * 10 + signal * (str[i] - '0');
            }else{
                return total;
            }
            
            if(total > max ){return max;}
            else if(total < min){return min;}
            
            ++i;
        }
        
        return total;
        
    };
    

    Can someone help me to figure out the reason? Thanks


Log in to reply
 

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