如何处理正负数以及溢出问题?
溢出时,循环中新的结果与上一个结果不相等,此时返回0.
public int reverse(int x)
{
int result = 0;
while (x != 0)
{
int tail = x % 10;
int newResult = result * 10 + tail;
if ((newResult - tail) / 10 != result)
{ return 0; }
result = newResult;
x = x / 10;
}
return result;
}
LeetCode里的7.反转整数问题 Java15行
最新推荐文章于 2022-04-17 01:07:12 发布