7、整数反转

输入321,需要输出123

public static int reverse(int x) {
int res = 0;
while (x != 0) {
// 下一步要res*10,所以这一步要保证res*10不大于 Integer.MAX_VALUE
if (Math.abs(res) > Integer.MAX_VALUE / 10) {
return 0;
}
res = res * 10 + x % 10;
x = x / 10;
}
return res;
}
posted @ 2024-01-04 17:29  MarkLeeBYR  阅读(11)  评论(0)    收藏  举报