您的位置:首页 > 其它

leetcode Reverse Integer

2015-01-26 10:54 399 查看
此题需要注意的地方是处理数字反转后溢出的情况

class Solution {
public:
int reverse(int x) {
if(x==0)
return x;

int res = 0;
int sign = 1;
if(x<0)
{
sign = -1;
x = abs(x);
}

while(x!=0)
{
if(res>(INT_MAX-x%10)/10)
return 0;

int digit = x%10;
res = res*10 + digit;
x = x/10;
}

return (sign==-1)? -res:res;

}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: