您的位置:首页 > 其它

[LeetCode]Reverse Integer

2013-10-10 02:39 375 查看
这题啥也不说了,简单…… 记一下代码。

要通过测试,主要就是处理一下正负号。

class Solution {
public:
int reverse(int x) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int result = 0;
int sign = x < 0 ? -1 : 1;
x *= sign;
while (x > 0) {
/*if (result == 214748364) {
if (sign == -1) {
if (x > 8) return 0;
} else {
if (x > 7) return 0;
}
} else if (result > 214748364) return 0;*/
result = (result * 10) + (x % 10);
x /= 10;
}
result *= sign;
return result;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode easy