# Very Short (7 lines) and Elegant Solution

• ``````public int reverse(int x) {
long rev= 0;
while( x != 0){
rev= rev*10 + x % 10;
x= x/10;
if( rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE)
return 0;
}
return (int) rev;
}``````

• with all due respect...

since you used long type, what if you are asked for reversing a 64-bit integer?

so i think we should check overflow instead of using larger data types.

• good question, but in Java integers are 32 bits, so we wouldn't have to worry about this case

• This post is deleted!

• I think you should not use Long in this sort of question

• @hassaan22 The same as positive. No difference.

• @hassaan22 If you understand, for example that -123%10 = -3 (not 3) than it will be easier

• @ranhar and -123/10 = -12

• @mach7 I used BigInteger instead of long..

• The code looks pretty cute.

• @mach7 my way to check overflow is
If(rev /10> Integer.Max ll rev / 10 < Integer.Min)
overflow

• i also do this :)

``````public int reverse(int x) {
long result = 0;
int digit = 0;

while (x != 0) {
digit = x % 10;
result = (result * 10 + digit);
x /= 10;
}

if(result > Integer.MAX_VALUE || result < Integer.MIN_VALUE){
return 0;
}

return (int)result;
}
``````

I thought last if should be out of the while, so I wrote this ;)

• @SoraShiro what if reverse number tyep is long but not int .

• Nice solution. It can be even shorter.

``````public int reverse(int x) {
long result = 0l;
while(x != 0) {
result = result * 10 + (x % 10);
x = x/10;
}
return (result < (long)Integer.MIN_VALUE || result > (long)Integer.MAX_VALUE)? 0:(int)result; // overflow
}``````

• In this solution

if( rev > Integer.MAX_VALUE || rev < Integer.MIN_VALUE)

can be replaced by

if(rev != (int) rev)

• @LLLLLxiaobei
Hi,
Your suggestion is incorrect because rev will be cast to int as well in order to perform an int comparison in the if statement. So, it will be always be equal.

Try run the code below as a proof: :)

``````long r= Integer.MAX_VALUE + 1;
if(r == (int)r){
System.out.println("EQUAL, which is wrong");
} else {
System.out.println("NOT EQUAL, which is incorrect");
}``````

• @kbakhit
Hi,

Did you try my suggestion? It is accepted by LeetCode.

• @kbakhit

@LLLLLxiaobei
Hi,
Your suggestion is incorrect because rev will be cast to int as well in order to perform an int comparison in the if statement. So, it will be always be equal.

Try run the code below as a proof: :)

``````long r= Integer.MAX_VALUE + 1;
if(r == (int)r){
System.out.println("EQUAL, which is wrong");
} else {
System.out.println("NOT EQUAL, which is incorrect");
}
``````

The example you gave here is also not appropriate. Here is the reason:

Integer.MAX_VALUE = 2147483647
Integer.MAX_VALUE + 1 = 2147483647 + 1 = -2147483648 = Integer.MIN_VALUE

Actually, r = Integer.MIN_VALUE, which means r is still in the int range, so "r == (int) r" is true.

• @kbakhit It is a good solution which uses short codes!

• This post is deleted!

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