Javascript solution


  • 1
    N
    /**
     * @param {number} x
     * @return {number}
     */
    var reverse = function(x) {
        if(x>(Math.pow(2,31)-1)) return 0;
        var reversed = parseInt((""+x).replace('-','').split('').reverse().join(''));
        return (reversed>(Math.pow(2,31)-1))?0:reversed*(x<0?-1:1);
    };
    

    Had to check for max to satisfy test cases. Any suggestion for improvements?


  • 1
    D
    var reverse = function(x) {
        var reverseNum=parseInt(x.toString().split("").reverse().join(""));
        
        if(reverseNum>(Math.pow(2,31)-1)) return 0;
        
        return x>=0?reverseNum:0-reverseNum;
    };
    

Log in to reply
 

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