LeetCode – Palindrome Number (Java)

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

Thoughts

Problems related with numbers are frequently solved by / and %. No need of extra space is required. This problem is similar with the Reverse Integer problem.

Note: no extra space here means do not convert the integer to string, since string will be a copy of the integer and take extra space. The space take by div, left, and right can be ignored.

Java Solution

public class Solution {
    public boolean isPalindrome(int x) {
        //negative numbers are not palindrome
		if (x < 0)
			return false;
 
		// initialize how many zeros
		int div = 1;
		while (x / div >= 10) {
			div *= 10;
		}
 
		while (x != 0) {
			int left = x / div;
			int right = x % 10;
 
			if (left != right)
				return false;
 
			x = (x % div) / 10;
			div /= 100;
		}
 
		return true;
    }
}
Category >> Algorithms  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>
  • jkw

    Not sure what kind of machine you are using but please actually run it. 10021 always return true in your code. Try it with hand and you’ll see that after the first loop you get x = 2.

  • Ankit Shah

    My code below just uses the reverse(num) function seen

    public class Palindromeclass {
    private static int reverse(int num) {
    int res = 0;
    while (num != 0) {
    res = (res * 10) + (num % 10);
    num = num / 10;
    }
    return res;
    }

    public static boolean isPalindrome(int num) {
    if (num < 0) return false;
    return num == reverse(num);
    }

    public static void main(String[] args) {
    System.out.println(isPalindrome(2332)); // true
    System.out.println(isPalindrome(4000)); // false
    System.out.println(isPalindrome(-1)); // false
    }
    }

  • cv

    Without using any variable at all :

    public class PalindromeInteger {

    static int palindrome= 1237321;

    static boolean checkPalindrome() {

    while(palindrome > 0) {
    if(!(palindrome%10 == palindrome/((int)(Math.pow(10,(int)(Math.log10(palindrome)+1)-1))))) {
    return false;
    }
    palindrome = (palindrome - (palindrome/((int)(Math.pow(10,(int)(Math.log10(palindrome)+1)-1))))* ((int)(Math.pow(10,(int)(Math.log10(palindrome)+1)-1))))/10;
    }

    return true;

    }

    public static void main(String[] args) {

    System.out.println("Palindrome to be guessed is "+palindrome+ ", Guessing : "+checkPalindrome());

    }

    }

  • qeatzy

    with one local variable

    public class Solution {
    public boolean isPalindrome(int x) {
    if(x<0) return false;
    if(x9;right*=10) {}
    for(;x;x=(x%right)/10,right/=100) {
    if(x/right!=x%10) return false;
    }
    return true;
    }
    };

  • amaurya

    It will throw runtime exception for 11,22…

  • ryanlr

    I think the extra space here means do not convert the integer to string, since string will be a copy of the integer and take extra space. div, left, and right can be ignored.

  • foo

    Either the question is badly phrased or you’re in violation by using extra space (div, left, right).

  • ryanlr

    It returns false for 10021 on my machine.

  • Wang

    How do you deal with 10021? It return true, but it should be false.

  • TK

    For 1234567899, if you reverse the integer, it overflows. Generally the overflowed number won’t be equal to the x, but it is uncertain.

  • Adj halibu

    That is a nice observation. You are right it causes an overflow when using
    Integer.MIN_VALUE. We can mitigate the problem using long local variables instead of int
    Thanks

  • Wang Yi

    It cannot handle x equals Integer.MIN_VALUE

  • Adj Halibu

    Here is a shorter and simpler version which handles negative numbers

    public static boolean PalInt(int x)
    {
    x = (x0)
    {
    sum=10*sum +temp%10;
    temp = temp/10;
    }
    return (sum == x)?true:false;
    }

  • Sam

    you can debug the code

  • Jocelyn

    what about 32123, after 3, 3, 2, 2, only left 1, 1!=0, left=0,right=1, then you will return false, however 32123 is palindrome.