把一个整数倒序排列(java)

问题:如 123———>321       -123————>-321    120————>21 怎么玩呢?

注意要考虑整数的范围是-231次方到231-1

public int reverse(int x) {
int rev = 0;
while (x != 0) {
int pop = x % 10;
x /= 10;
if (rev > Integer.MAX_VALUE/10 || (rev == Integer.MAX_VALUE / 10 && pop > 7)) return 0;
if (rev < Integer.MIN_VALUE/10 || (rev == Integer.MIN_VALUE / 10 && pop < -8)) return 0;
rev = rev * 10 + pop;
}
return rev;
}

想要玩更多的?请到 https://leetcode.com/explore/

原文地址:https://www.cnblogs.com/thg999/p/9936276.html