整数翻转123--->321

代码:

class Solution {
public static 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;
}

public static void main(String [] args){
System.out.println(reverse(123));
}

}

猜你喜欢

转载自blog.csdn.net/xiongxianze/article/details/79412265