Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
这题注意两个问题就OK,一是能够被10整除时,后面的零要先被去掉;第二个就是溢出的处理!
class Solution {
public:
int reverse(int x) {
if (x == 0)
return 0;
long long sum = 0;
while (x % 10 == 0)
{
x /= 10;
}
do{
sum = 10 * sum + x % 10;
x /= 10;
} while (x!=0);
return (sum<INT_MAX && sum>INT_MIN) ? (int)sum : 0;
}
};