Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
解题思路在于利用 t = res * 10 + x % 10 来讲数字一位一位左移,而在int超过最大值之后会变,所以逆过来用来验证。
public class Solution {
public int Reverse(int x) {
int res = 0;
while (x != 0) {
int t = res * 10 + x % 10;
if ((t - x % 10 ) /10 != res) return 0;
res = t;
x /= 10;
}
return res;
}
}