Simple AC solution in Java ( 13 lines )


  • 0
    S
    public int reverse(int x) {
            boolean negative = false;
            if ( x < 0 ) negative = true;
            x = Math.abs( x );
            int max = Integer.MAX_VALUE / 10;
            int result = 0;
            while( x > 0 ) {
                if ( result > max) return 0;
                result = result * 10 + ( x % 10 );
                x = x / 10;
            }
            if ( negative ) return -result;
            return result;
        }

Log in to reply
 

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