题目的意思是判断数字是否为回文。本文用了两种方法,一种效率稍高,但代码稍稍多几行;一种代码稍少,但效率稍稍低点。
题目:
我的第一种解决方案:
public class Solution {
public boolean isPalindrome(int x) {
boolean is=true;
if(x>=0){
//Integer xi=x;
//String s=xi.toString();
String s=x+"";
int len=s.length();
int mid=0;
if(len%2==0){mid=len/2;}else{mid=(len-1)/2;}
for(int i=0;i<mid;i++){
if(s.charAt(i)!=s.charAt(len-i-1)){is=false;break;}
}
}else{
is=false;
}
return is;
}
}
效率:
我的第二种解决方案:
public class Solution {
public boolean isPalindrome(int x) {
int temp=x;
if(temp<0){
return false;
}else{
int y=0;
while(temp>0){
y=y*10+temp%10;
temp/=10;
}
return x==y;
}
}
}