Question
Reverse digits of an integer.
Examples:
x = 123, return 321 x = -123, return -321
Solution
Result: Accepted Time: 8ms
Here should be some explanations.
int reverse(int x) {
int flag = 1;
const int limit = 0x7fffffff / 10 ;
int ans=0,last = 0;
if(x < 0)
{
flag = -1;
if(x == 0x80000000)
return 0;
x = -x;
}
while(x)
{
if(last > limit )
return 0;
ans= ans*10 + x%10;
x/=10;
last = ans;
}
return ans*flag;
}
Complexity Analytics
- Time Complexity:
- Space Complexity: