题目
翻转整数
分析
1 Java里整数的范围是多少 - 2 32 ~ 2 32 - 1 (有0存在) Integer.MAX_VALUE
2 越界问题 通常用long型来避免越界的问题
代码
class Solution {
public int reverse(int x) {
long res = 0;
while (x != 0){
res = res * 10 + x%10;
x = x/10;
if(res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) return 0;
}
return (int)res;
}
}