Reverse Integer Java


  • 0
    A
     public int reverse(int x) {
            int sign = x < 0 ? -1 : 1;
            x = Math.abs(x);
            int rev = 0;
            while (x > 0) {
                if ((long) rev * 10 > Integer.MAX_VALUE) return 0;
                rev = rev * 10 + x % 10;
                x /= 10;
            }
            return rev * sign;
        }
    

Log in to reply
 

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