I have the following code w/o taking care of the overflow, but it's got AC.
int reverse(int x) {
int sign=x>0?1:-1;
x=x*sign;
int rev=0;
while(x!=0) {
rev=10*rev+x%10;
x/=10;
}
return rev*sign;
}
Even this overflow unhandled code got accepted. There is no overflow check in the test cases used.
public class Solution{
public int reverse(int x) {
boolean flag = false;
if (x < 0) {
x = 0 - x;
flag = true;
}
int res = 0;
int p = x;
while (p > 0) {
int mod = p % 10;
p = p / 10;
res = res * 10 + mod;
}
if (flag) {
res = 0 - res;
}
return res;
}
}