文章目录

Reverse digits of an integer.

Example1: x = 123, return 321

Example2: x = -123, return -321


递归处理,代码可以写的很优雅

1
2
3
4
5
6
7
8
9
10
public int reverse(int x) {
boolean neg = x < 0;
if (neg) x = -x;
int result = 0;
while (x != 0) {
result = result * 10 + x % 10;
x /= 10;
}
return (neg ? -1 : 1) * result;
}
文章目录