public boolean isPalindrome(int x) {
//reverse integer,and check whether the same
if(x<0) return false;
int newx=0;
int m=x;
while(x!=0){
newx=newx*10+x%10;
x=x/10;
}
return m==newx;
}
上述算法的缺点:循环次数多,优化:n ==》n/2
public boolean isPalindrome(int x) {
//计算reverse integer
if(x<0||(x>0 && x%10==0)) return false;
int newx=0;
while(x>newx){
newx=newx*10+x%10;
x=x/10;
}
return (x==newx||x==(newx/10));
}
本文介绍了两种判断整数是否为回文数的方法,并对第二种方法进行了优化,减少了循环次数,提高了算法效率。
616

被折叠的 条评论
为什么被折叠?



