文章--LeetCode算法--ReverseInteger
ReverseInteger
问题描述
Reverse digits of an integer.
实例
- Example1: x = 123, return 321
- Example2: x = -123, return -321
实现代码
public 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;
}
}