Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
题目比较简单,给出的函数原型int reverse(int x);
这里让我费解的地方是展开讨论的这句话:Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases? 就算我申请了long long 或者 用一个数组来存那个Reverse之后越界的玩意儿,可是你返回值还是int,而且好像cases里面也没有卡这个问题,下面是我的代码。
class Solution {
public:
int reverse(int x) {
int Reverse = 0;
while (x != 0){
Reverse = Reverse * 10 + x % 10;
x = x / 10;
}
return Reverse;
}
};
这里不需要判断正负,正负号没区别,结果一样。