-1

it is actually somewhat related to the question Reverse Integer leetcode -- how to handle overflow, to reverse digits of an integer, while returning 0 when the reversed integer overflows.

so it is supposed to be:

input: -123, return: -321

input:1500032455, return: 0

I could reverse the digits for the normal case but not the overflow case, I checked with the link above, mine is a little bulky..but I guess it should work the same? Could anyone advise?

    public int reverse(int x) {

        boolean isPositive = true;
        if (x < 0){
            isPositive = false;
            x = x * -1;
        }

        int length = String.valueOf(x).length();
        int[] digit = new int[length];

        for (int i = 0; i < length; i++){
            digit[i] = x % 10;
            x = x / 10;
        }

        int solution = 0;
        for (int j = 0; j < length; j++){
            solution = solution + digit[length-j-1] * (int) Math.pow(10,j);
        }

        if (solution > Integer.MAX_VALUE){
            return 0;
        }

        return (isPositive == false) ? -solution : solution;        
    }

}
blossomvy
  • 55
  • 10
  • 2
    solution is an int. By definition of MAX_INTEGER, an int can't be bigger than MAX_INTEGER, otherwise, it wouldn't be the max value of an integer, would it? If you want a number that can be bigger than MAX_INTEGER, you need another type than `int`. There aren't that many in Java, so I'll let you figure that out. – JB Nizet Aug 03 '19 at 12:39
  • Sometimes int when exceeding the maximum value seems to rewind (go to negative) or even random numbers are produced so we can't trust in to become MAX_INTEGER. – Rohit Aug 03 '19 at 17:10
  • Thank you!! i should have created solution as long then! – blossomvy Aug 04 '19 at 08:18
  • "long" solved most of the cases, however, while testing for -2147483648, it doesn't work....there I found that after the first part (checking if it is positive, and convert the x to a positive number if it was not..), the value of x is still a negative after this... – blossomvy Aug 04 '19 at 11:20

1 Answers1

0

I think there is a much simpler way of doing this. Try :

int reverse(int num)
{
    char[] number = String.valueOf(num > 0 ? num : -num).toCharArray();
    for(int i=0; i< number.length/2;i++)
    {
        char temp = number[i];
        number[i] = number[n-i];
        number[n-i] = temp;
    }
    BigInteger Num = new BigInteger(new String(number));
    if(Num.compair(BigInteger.valueOf(Integer.MAX_INTEGER)) >= 0) return 0; // Or throw an Exception.
    else return Integer.praseInt(new String(number)) * (num >0 ? 1 : -1);
}

You can use this to reverse numbers greater than Integr.MAX_INTIGER by simply replacing the return type of the function and changing the last if statement.

Rohit
  • 495
  • 9
  • 16
  • I am not sure weather BigIntegr.valueOf() takes int or float as argument either way it will work. (If it is float just cast int to float). If you need any more explanation I would be happy to help. – Rohit Aug 03 '19 at 17:07