Reverse Integer
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
class Solution {
public:
int reverse(int x) {
if (x>(numeric_limits<int>::max)()||x<(numeric_limits<int>::min)())
{
return 0;
}
int base=x;
bool flag;
if(x>0)
flag=true;
else
flag=false;
int count=1;
while (base=base/10)
count++;
int answer=0;
x=abs(x);
while(count>0)
{
int yushu=x%10;
x=x/10;
answer+=yushu*pow(double(10),--count);
if (answer<0)
{
return 0;
}
}
if (flag)
{
return answer;
}
else
{
return 0-answer;
}
}
};
测试时出现的问题:
class Solution {
public:
int reverse(int x) {
if (x>(numeric_limits<int>::max)()||x<(numeric_limits<int>::min)())
{
return 0;
}
int base=x;
bool flag;
if(x>0)
flag=true;
else
flag=false;
int count=1;
while (base=base/10)
count++;
int answer=0;
x=abs(x);
while(count>0)
{
int yushu=x%10;
x=x/10;
answer+=yushu*pow(double(10),--count);
if (answer<0)
{
return 0;
}
}
if (flag)
{
return answer;
}
else
{
return 0-answer;
}
}
};