自己写的代码:
public class Solution {
public int reverse(int x) {
int tmp = Math.abs(x);
String str=String.valueOf(tmp);
StringBuilder str1=new StringBuilder(str);
str1.reverse();
String str2=str1.toString().toLowerCase();
x = Integer.parseInt(str2);
if(x<0) x=-x;
return x;
}
}
又报和第9题,相同的错误:
参考代码:
public class Solution {
public int reverse(int x) {
long result = 0;
int tmp = Math.abs(x);
while(tmp>0){
result *= 10;
result += tmp % 10;
if(result > Integer.MAX_VALUE){
return 0;
}
tmp /= 10;
}
return (int)(x>=0?result:-result);
}
}