import java.util.*; public class Solution { /** * * @param x int整型 * @return int整型 */ public int reverse (int x) { // write code here int res = 0; while (x != 0) { // 前一个结果值 int preRes = res; // 获取最后一位数 int mod = x % 10; // 反向主键乘数结果值 res = preRes * 10 + mod; if ((res - mod) / 10 != preRes) return 0; // 消除最后一位数 x /= 10; } return res; } }
解题思想:反向结果值乘数+模个数,避免溢出通过反向结果值判断。