力扣第七题:整数反转
解题思路:
首先是整数反转代码实现:
每次取最后一位
res=res*10+x%10;
x=x/10;
快乐数求和与他类似
sum+=x%10*x%10;
x=x/10;
其次是范围划定是否溢出
如果换算回去结果不变的话,就证明没有溢出
class Solution {
public int reverse(int x) {
int res=0;
int t=0;
int newres;
while(x!=0){
t=x%10;
newres=res*10+t;
if((newres-t)/10!=res){
return 0;
}
res=newres;
x=x/10;
}
return res;
}
}