Acceptable JavaScript solution


  • 0
    N
    function reverse(x) {
        
        var string = x.toString();
        var array = [];
        for(var i=0; i<string.length; i++) {
            array.unshift(string[i]);
            // Alternatively, start push() from i=n and decrement.
        }
        
        if (x<0) array.unshift('-')
        var joined = parseInt(array.join(''));
        
        // Maximum integer here is: Math.pow(2, 31) - 1, or 2,147,483,657
        if(joined > 2147483647 || joined < -2147483647) return 0
        else return joined;
    }
    

Log in to reply
 

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