用自己的方法:将x转换为String,再进行反转,最后再转化为int类型
public class Solution {
public boolean isPalindrome(int x) {
if(x<0)
return false;
int temp=x;
String str=String.valueOf(x);
StringBuilder str1=new StringBuilder(str);
str1.reverse();
String str2=str1.toString();
x = Integer.parseInt(str2);
return (temp==x?true:false);
}
}
报出这个错误:
Runtime Error Message:
Line 11: java.lang.NumberFormatException: For input string: "7463847412"
Last executed input:
2147483647
最后参考
public class Solution {
public boolean isPalindrome(int x) {
if (x < 0) return false;
int div = 1;
while (x / div >= 10) div *= 10;
while (x > 0) {
int left = x / div;
int right = x % 10;
if (left != right) return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
}
网友评论