[leedcode 09]Palindrome Number

public class Solution {
    public boolean isPalindrome(int x) {
        //负数不是回文
        //主要的突破点是如何获得整数的第n位和后n位(/和%运算结合)
        if(x<0)return false;
        int index=1;
        int temp=x;
        while(temp>=10){//这里面需要考虑整数越界的问题,不能跟0比较
            temp/=10;
            index*=10;
        }
       
        //index=index/10;
        
        /*while(x/index>=10){
            index*=10;
        }*/
        while(x>0){
            int left=x/index;
            int right=x%10;
            if(left==right){
                x=x%index;
                x=x/10;
                index/=100;//注意去掉首尾位的方法,比如1331,需要转化成33
            }else return false;
            
            
        }
         return true;
        
        
    }
}
原文地址:https://www.cnblogs.com/qiaomu/p/4625110.html