标签:
Reverse digits of an integer.
Example1: x = 123, return 321
Example2: x = -123, return -321
注意溢出,溢出返回0
public class Solution {
public int reverse(int x) {
int sign = 1;
if (x < 0) {
sign = -1;
x = -x;
}
long y = 0;
while (x > 0) {
y = y * 10 + x % 10;
x = x / 10;
}
int ret = (int) y;
if (y != ret) {
return 0;
}
return ret * sign;
}
}
标签:
原文地址:http://www.cnblogs.com/shini/p/4467917.html