Easy 9题 Palindrome Number

来源:互联网 发布:软件外包人员管理 编辑:程序博客网 时间:2024/05/17 06:37

Question:

Determine whether an integer is a palindrome. Do this without extra space.

Solution:

调试半天。。。但居然过了61%

public class Solution {    public boolean isPalindrome(int x) {           if(x<0)            return false;        if(x==0)            return true;        int tmp=x;        int count=1;        while(tmp/10!=0)        {            tmp=tmp/10;            count++;        }        for(int i=1,j=count;i<j;i++)        {            int tmp_1=(int) (x%(Math.pow(10,1)));            int tmp_2=(int) (x/(Math.pow(10,j-1)));            //测试前面有几个零            int ii=1;            int jj=j-2;            int cou=1;            while((int)(x/(Math.pow(10,jj)))==Math.pow(10,ii))            {                ii++;                jj--;                cou++;            }            int tmpp=(int) (x-tmp_2*Math.pow(10,j-1)-tmp_1);            if((int) tmpp%Math.pow(10,cou)!=0)                return false;            x= (int) (tmpp/Math.pow(10,cou));            if(tmp_1!=tmp_2)                return false;            j=j-cou*2;        }        return true;    }}

discuss上的解法

        if(x<0||(x!=0&&x%10==0))            return false;        int rev=0;        while(x>rev)        {            rev=rev*10+x%10;            x=x/10;        }        return (x==rev||x==rev/10);



0 0