问题:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
这个问题比较简单,但非常值得吐槽,因为题目没有约定越界应该怎么处理。
代码示例:
public class Solution {
public int reverse(int x) {
long result = 0;
while (x != 0) {
result = result * 10 + x % 10;
if (result > Integer.MAX_VALUE || result < Integer.MIN_VALUE) {
return 0;
}
x = x / 10;
}
return (int)result;
}
}