题目:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
思路:这道题的关键点就在于当反转之后溢出的判断。
代码如下:
class Solution {
public int reverse(int x) {
//int的范围-2147483648~+2147483647
int res=0;
while(x!=0){
int tail=x % 10;
int newRes=res*10+tail;
if((newRes-tail)/10!=res)
return 0;
res=newRes;
x/=10;
}
return res;
}
}