Short solution in JavaScript


  • 0
    L
    /**
     * @param {number} x
     * @return {number}
     */
    var reverse = function (x) {
      if (Math.abs(x) < 10) { // Digit in ones can't be reverse.
        return x;
      } else {
        var t = Number(Math.abs(x).toString().split('').reverse().join(''));
        // Take absolute value > Convert to string > Convert to array > Reverse array > Convert to string > Convert to number
        return t < (Math.pow(2, 31) - 1) ? (x < 0 ? 0 - t : t) : 0;
        // Verify the number is 32-bit signed integer, Positive and negative fix.
      }
    };
    

Log in to reply
 

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