您的位置:首页 > 编程语言 > Java开发

LeetCode里的7.反转整数问题 Java15行

2017-12-30 18:23 423 查看
如何处理正负数以及溢出问题?
溢出时,循环中新的结果与上一个结果不相等,此时返回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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: