Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
解法:
这里需要讨论整数是否overflow。
int reverse(int x)
{
const int MAX_INT = ((unsigned)(-1)>>1);
//cout<<MAX_INT<<endl;
const int MIN_INT = (~MAX_INT);
//cout<<MIN_INT<<endl;
int re=0;
while(x!=0)
{
int tmp=x%10;
if(re>MAX_INT/10 || re<MIN_INT/10)
return 0;
if(tmp>0)
if(re*10>MAX_INT-tmp)
return 0;
if(tmp<0)
if(re*10<MIN_INT-tmp)
return 0;
re=re*10+tmp;
x=(int)x/10;
}
return re;
}