您的位置:首页 > 其它

[Leetcode]Reverse Integer

2015-08-12 22:24 381 查看
Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

class Solution {
public:
/*algorithm
if x is neg numer, and -x overlfows, return 0
otherwise, x = -x
for x, when reverse x ,it can overflow, at that time, return 0
*/
int reverse(int x) {
int sum = 0;
while(x != 0){
if(abs(sum) > INT_MAX/10)return 0;
sum = sum*10 + x%10;
x /= 10;
}
return sum;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode 算法