解题思路:
依次取出当前数字的最后一位数,累加。
关键在于如何判断整数溢出:将每次操作后的数字用临时变量存储,对该变量“反操作”,若与操作前的结果不等,则发生溢出,直接返回0
源代码:
class Solution {
public int reverse(int x) {
int res = 0 ;
while(x != 0){
int temp = x % 10 + res * 10;
if((temp - x % 10) / 10 != res){ //最关键的一步
return 0 ;
}
res = temp ;
x /= 10 ;
}
return res ;
}
}