对数字取余数和取整数,余数*10+整数取余
import java.util.*;
public class Solution {
/**
*
* @param x int整型
* @return int整型
*/
public int reverse (int x) {
// write code here
long result = 0;
// 还有数字没有反转完时
while (x != 0) {
// * 10 是为了将已经发转的数字向左移一位。
// % 10 是为了只留下 个位数
result = result * 10 + x % 10;
// 如果结果溢出了,就直接返回 0
if (result > Integer.MAX_VALUE) {
return 0;
}
// 剔除掉已经反转后的 个位
x = x / 10;
}
return (int)result;
}
}

京公网安备 11010502036488号