Reverse Integer Java


  • 0
    R

    public static int reverse(int x) {
    int ans = 0, curr = 0;
    int input = x;
    if(x == Integer.MIN_VALUE) {
    return 0;
    }
    while(Math.abs(input) >= 10){
    System.out.println(Math.abs(input) >= 10);
    curr = input%10;
    System.out.println("curr"+curr);
    if(Integer.MAX_VALUE/10 == Math.abs(ans) && Integer.MAX_VALUE%10 < curr){
    System.out.println(Integer.MAX_VALUE/10 == Math.abs(ans) && Integer.MAX_VALUE%10 < curr);
    return 0;

            }
            
            ans = ans*10 + curr;
            input = input / 10;
            if(Integer.MAX_VALUE / 10 < Math.abs(ans)) {
            	System.out.println(Integer.MAX_VALUE / 10 < ans);
            	System.out.println((Integer.MAX_VALUE - Math.abs(x)) < Math.abs(ans * 10));
            	System.out.println("xxx"+(Integer.MAX_VALUE - Math.abs(x)));
            	System.out.println(Math.abs(ans * 10));
            	return 0;
            }
        }
        if(Math.abs(input)<10){
            ans = ans*10+input;
        }
        return ans;
    }

Log in to reply
 

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