题目链接:https://leetcode.com/problems/reverse-integer/
题目:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
注意:
判断输入数据是否在整数范围内。(Integer.MIN_VALUE,Integer.MAX_VALUE)
public class Solution {
public int reverse(int x) {
if (x == Integer.MIN_VALUE)
return 0;
int num = Math.abs(x);
int res = 0;
while (num != 0) {
if (res > (Integer.MAX_VALUE - num % 10) / 10)
return 0;
res = res * 10 + num % 10;
num = num / 10;
}
return x > 0 ? res : -res;
}
}
1032 / 1032 test cases passed.
Status: Accepted
Runtime: 312 ms