[LeetCode-Java]9. Palindrome Number

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

解:分离相对应的两位进行比较,不过大量数学计算比较耗时。

public boolean isPalindrome(int x) {
        boolean isPalindrome = true;

        int length = 1;

        int a =0,b=0;

        if (x<0){
            isPalindrome = false;
        }else {
            int y = x;
            //求x的位数
            while (y/10 != 0){
                length++;
                y = y/10;
            }

            for (int i = 1;i<=length/2;i++){

                a = (int)(x%(Math.pow(10,length-i+1)))/(int)Math.pow(10,length-i);
                b = (int)(x%(Math.pow(10,i)))/(int)(Math.pow(10,i-1));

                if ( a != b){
                    isPalindrome = false;
                    break;
                }
            }
        }



        return isPalindrome;
    }

你可能感兴趣的:(LeetCode)