查看算法描述

算法实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {   
public int reverse(int x) {
int one = x>0?1:-1;
x = x>0?x:-x;
long result = 0;
for (; x > 0; ) {
int remainder = x % 10;
result += (long)Math.pow(10, x<10 ? 0:String.valueOf(x).length()-1) * remainder;
x = x / 10;
}
result = (long)result*one;
return result>Integer.MAX_VALUE||result<Integer.MIN_VALUE?0:(int)result;
}
}