您的位置:首页 > 其它

LeetCode之Reverse Integer

2013-12-25 12:04 459 查看
http://oj.leetcode.com/problems/reverse-integer/

数字反转,不过点击那个  click to show spoilers. ,会出现一些面试的“剧透”,

因为LeetCode是为面试服务的,所以即使题目AC掉了,还是有很多坑可以调的。

比如这道题的坑就有: 末尾是0的反转结果是啥,如果反转后整型溢出了怎么办( 1000000003 ),如果需要

处理这种溢出情况,怎么处理?是throw 一个exception还是返回extra paramter。这些都是需要进行考虑的。

当然:题目按照原始的方法做就能ac掉的

class Solution {
public:
int reverse(int x) {
int isPositive = 1;
int reverseDigit = 0;

if(x < 0) {
isPositive = -1;
x = -x;
}

while(x/10) {
reverseDigit = 10 * reverseDigit + x%10;
x /= 10;
}
reverseDigit = 10 * reverseDigit + x%10;
return reverseDigit * isPositive;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: