难度等级: Easy
关键字: Math
相似问题: (E) String to Integer
题目说明:
Reverse digits of an integer.
样例输入输出:
Example1: x = 123, return 321
Example2: x = -123, return -321
题解:
public class Solution {
public int reverse(int x) {
int res = x;
int [] cout = new int[11];
int i = 0;
if(x > 0)
res = x;
else
res = -x;
while(res > 0) {
cout[i] = res % 10;
res = res / 10;
i++;
}
res = 0;
for(int j = 0; j < i; j++) {
if(res > (Integer.MAX_VALUE - cout[j]) / 10)
return 0;
res = res * 10 + cout[j];
}
if(x > 0)
return res;
else
return -res;
}
}