Solution in JS


  • 0
    R

    We need to find whether the number is a 32- bit range integer or not so validated the same in the first line.
    Range ===< -2147483647 <number&& reveredNumber < 2147483647.

    after reversing the number using simple while loop check the same Range condition and return the number.

    Thanks.

    ...
    /**

    • @param {number} x

    • @return {number}
      */
      var reverse = function (number) {
      // number = 1534236469;
      if (number < -2147483647 || number > 2147483647) {
      return 0;
      }

      var reversed = 0;

      while (number != 0) {
      reversed *= 10;

       reversed += number % 10;
      
       number -= number % 10;
      
       number /= 10;
      

      }

      if (reversed < -2147483647 || reversed > 2147483647) {
      return 0;
      }

      return reversed;
      };..


Log in to reply
 

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