Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
Subscribe to see which companies asked this question
代码:
class Solution {
public:
int reverse(int x) {
if(x==INT_MIN) return 0;
bool index=false;
if(x<0) {index=true;x=abs(x);}
double sum=0;
while(x>=10)
{
sum=sum*10+x%10;
x=x/10;
}
sum=sum*10+x;
if(sum>INT_MAX) return 0;
return index?(-1*sum):sum;
}
};