class Solution {
public int reverse(int x) {
int result = 0;
while(x != 0) {
if(result > 0 && result > (Integer.MAX_VALUE - x % 10) / 10) return 0;
if(result < 0 && result < (Integer.MIN_VALUE - x % 10) / 10) return 0;
result = result * 10 + x % 10;
x /= 10;
}
return result;
}
//使用long
public int reverse2(int x) {
long ans = 0;
while(x!=0) {
ans = ans * 10 + (x % 10);
x /= 10;
}
return ans >=Integer.MAX_VALUE || ans <= Integer.MIN_VALUE? 0 : (int)ans;
}
}
7. 整数反转
于 2018-08-15 01:25:49 首次发布