My java solution with overflow check by max/10+mod and min/10+mod


  • 0
    A

    Easy understand.

     public int reverse(int x) {
            int value=0;
            while(x!=0){            
                int mod = x%10;
                if((value<Integer.MAX_VALUE/10 && value>Integer.MIN_VALUE/10) ||
                   (value==Integer.MAX_VALUE/10&&mod<=7)
                   || (value==Integer.MIN_VALUE/10&&mod<=8))
                value=value*10+mod;
                else x=value=0;
                x=(x/10);           
            }
                return value;
        }
    

Log in to reply
 

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