天天看點

#9 LeetCode——Palindrome Number回文數字,即77、242、3993、12321之類的數字稱之為回文數字

回文數字,即77、242、3993、12321之類的數字稱之為回文數字

題目的難點在于不允許使用額外的空間,隻能将數字的首位挨個比較。

java代碼如下

public class Solution {
    public boolean isPalindrome(int x) {
        if(x < )
        {
            return false;
        }
        else
        {
            int len = , num = x;
            int leftIndex = , rightIndex = , left, right;
            while(num > )
            {
                num = num / ;
                len++;
                if(leftIndex == )
                {
                    leftIndex = ;
                }
                else
                {
                    leftIndex *= ;
                }
            }
            num = x;
            for(int i = ; i < len / ; i++)
            {

                left = (num / leftIndex) % ;
                right = (num / rightIndex) % ;
                leftIndex /= ;
                rightIndex *= ;
                if(left != right)
                {
                    return false;
                }
            }
            return true;
        }
    }
}