7.Reverse digits of an integer.
Problem descriptions:
Example1: x = 123, return 321
Example2: x = -123, return -321
Difficulty:Easy
class Solution {
public:
int reverse(int x) {
long long int y= 0,z=0;
int res = 0;
z = x;
if(x<0)
{
z = -z;
}
y = z%10;
z = z/10;
while(z>0)
{
y = y * 10 + z%10;
z = z /10;
}
if(x<0)
{
y = -y;
}
if (y >= INT_MAX)
{
res = 0;
}
else if (y<= INT_MIN)
{
res = 0;
}
else
{
res = (int)y;
}
return res;
}
};
注意正负、取值范围就OK了