Reverse Integer - Solution in Javascript


  • 0
    C
    /**
     * @param {number} x
     * @return {number}
     */
    var reverse = function(x) {
        var symbol = 1;
        if (x < 0) {
            symbol = -1;
            x = -x;
        }
        
        //reverse the num
        var num = x.toString().split("").reverse().join("");
        
        // if the num is overflow, return 0
        if (num > Math.pow(2,31)) {
            return 0;
        };
        
        return symbol * num;
    };
    

Log in to reply
 

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