承诺的事,就算拼了命也要做到啊,要不然承诺还有什么意义!
LeetCode官网:https://leetcode.com/
我是直接用GitHub授权登录的。
问题
给定一个32位有符号整数,整数的反转数字。
举例
Input: 123
Output: 321
Input: -123
Output: -321
Input: 120
Output: 21
解法
class Solution {
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;
}
}