JavaScript solution


  • 0
    R

    1032 / 1032 test cases passed.
    Status: Accepted
    Runtime: 142 ms

    /**
     * @param {number} x
     * @return {number}
     */
    var reverse = function(x) {
      var MAX_INTEGER = 2147483647;
      var result = 0;
      var isNegativeNumber = Math.abs(x) !== x;
      x = Math.abs(x);
    
      while (x !== 0) {
        result = result * 10 + x % 10;
        x = Math.floor(x / 10);
      }
    
      return result > MAX_INTEGER ? 0 : (result * (isNegativeNumber ? -1 : 1));
    };
    

Log in to reply
 

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