Problem:
Determine whether an integer is a palindrome. Do this without extra space.
简单的回文数,大一肯定有要求写过,不过从基础开始尝试吧。
Solution:
public class Solution { public boolean isPalindrome(int x) { int n=1; int copyx=x; if(x<0)return false; if(x<10)return true; while(x>=10){ x=x/10; n++; if(x<10)break; } if(n%2==0) { while(copyx/(int)Math.pow(10,n-1)==copyx%10) { copyx=copyx%(int)Math.pow(10,n-1); copyx=copyx/10; // if(x==0)return true; n=n-2; if(n==0)return true; } } if(n%2==1) { while(copyx/(int)Math.pow(10,n-1)==copyx%10) { copyx=copyx%(int)Math.pow(10,n-1); copyx=copyx/10; // if(x==0)return true; n=n-2; if(n==1)return true; } } return false; } }