Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
click to show spoilers.
解决代码:
class Solution {
public:
int reverse(int x) {
long long temp = abs((long long)x);
long long curr = 0;
while (temp)
{
curr = curr * 10 + temp % 10;
if (curr > INT_MAX)
{
return 0;
}
temp /= 10;
}
if (x >= 0)
return curr;
else
return -curr;
}
};