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>

  1. Jocelyn on 2014-4-25

    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.

  2. Sam on 2014-5-29

    you can debug the code

  3. Adj Halibu on 2014-9-17

    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;
    }

  4. Wang Yi on 2014-9-27

    It cannot handle x equals Integer.MIN_VALUE

  5. Adj halibu on 2014-9-27

    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

  6. TK on 2014-10-1

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

  7. Wang on 2014-10-22

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

  8. ryanlr on 2014-10-22

    It returns false for 10021 on my machine.

  9. foo on 2015-2-21

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

  10. ryanlr on 2015-2-23

    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.

  11. amaurya on 2015-7-21

    It will throw runtime exception for 11,22…

  12. qeatzy on 2015-7-28

    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;
    }
    };

  13. cv on 2015-8-18

    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());

    }

    }

  14. Ankit Shah on 2016-2-7

    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
    }
    }

  15. jkw on 2016-6-16

    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.

  16. Titan on 2017-1-16

    It wont work for : System.out.println(isPalindrome(2147483647)); // Overflow issues

  17. Danail Kozhuharov on 2017-4-11

    Here is a solution that does twice less arithmetic operations and handles Integer.MAX_VALUE:

    public class NumberPalindrome {
    public static void main(String[] args) {
    System.out.println(isPalinum(-1) + " false");
    System.out.println(isPalinum(0) + " true");
    System.out.println(isPalinum(9) + " true");
    System.out.println(isPalinum(110) + " false");
    System.out.println(isPalinum(11) + " true");
    System.out.println(isPalinum(101) + " true");
    System.out.println(isPalinum(1234554321) + " true");
    System.out.println(isPalinum(Integer.MAX_VALUE) + " false");
    }

    public static boolean isPalinum(int number) {
    if (number < 0) return false; // no negative palindromes
    if (number = tail) { // stops after half the digits are processed
    int digit = number % 10;
    if (digit == 0 && tail == 0) return false; // for nums ending in 0
    number = number / 10;
    if (number == tail) return true; // for odd num of digits
    tail = tail*10 + digit;
    if (number == tail) return true; // for even num of digits
    }
    return false; // not a palindrome
    }
    }

Leave a comment

*