Reverse digits of an integer.
Example1: x = 123, return 321 Example2: x = -123, return -321
Solution 1: Direct Reverse
Construct the new number directly
- take care of sign
- using
long long
in case of overflow
Code:
int reverse(int x) {
if(x == 0) return 0;
int sign = x>0 ? 1:-1;
long long t = x;
t = abs(t);
long long res = 0;
while(t != 0)
{
res = 10*res + t%10;
t /= 10;
}
res = sign*res;
return (res>INT_MAX || res<INT_MIN) ? 0:(int)res;
}