public static void main(String[] args) {
// int x = 1534236469;
//
// int res = reverse(x);
// System.out.println(res);
}
public static int getReverse(int x) {
int result = 0;
// 防止溢出
if (x > Integer.MAX_VALUE / 10 || x < Integer.MIN_VALUE / 10) {
return 0;
}
//x取余数不为0就一直循环运算
while (x != 0) {
//每次取最后一位
int endNumber = x % 10;
//缩小一位
x = x / 10;
//计算result:
result = result * 10 + endNumber;
}
return result;
}
/**
* 整数反转
* @param x
* @return
*/
public static int reverse(int x) {
int rev = 0;
while (x != 0) {
if (rev < Integer.MIN_VALUE / 10 || rev > Integer.MAX_VALUE / 10) {
return 0;
}
int digit = x % 10;
x /= 10;
rev = rev * 10 + digit;
}
return rev;
}
整数反转j
最新推荐文章于 2025-12-28 16:49:38 发布
415

被折叠的 条评论
为什么被折叠?



