ShanXiangJC
11/24/2017 - 2:56 PM

[9. Palindrome Number] #Array #Mod #Leetcode #Easy

[9. Palindrome Number] #Array #Mod #Leetcode #Easy

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


    // char[] method
    public  boolean isPalindrome(int x) {
        if (x < 0)
            return false;
        if (x < 10)
            return true;

        String s = Integer.toString(x);
        char[] c = s.toCharArray();


        for (int i = 0, j = c.length - 1; (i <= j); i++, j--) {
            if (c[i] != c[j])
                return false;
        }
        return true;
    }


    // get the tail method
    public  boolean isPalindrome(int x) {
        int ori = x;
        if (x < 0 || x % 10 == 0)
            return x == 0;
        if (x < 10)
            return true;

        long result = 0;
        while (ori > 0) {
            result = result * 10 + ori % 10;
            ori /= 10;
        }

        return (long) x == result;
    }