JavaScript solution (without using `.reverse()`)


  • 0
    A
    function reverse(n) {
      const MAX = 2147483647;
      const sign = n < 0 ? -1 : 1;
    
      let result = 0;
    
      for (let i = Math.abs(n); i >= 1; i = Math.floor(i / 10)) {
        result = (result * 10) + (i % 10);
      }
    
      return result < MAX ? (result * sign) : 0;
    }
    

Log in to reply
 

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