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

24 thoughts on “LeetCode – Palindrome Number (Java)”

  1. public static boolean isPalindrome(int x) {

    String number = String.valueOf(x);

    String reverse = new StringBuffer(number).reverse().toString();

    return number.equals(reverse);

    }

  2. same w/ recurse

    3113
    start divide /10- 3 1 1 3, then backtrack , start calc number, each step *10+ modulo
    in the end must be same numbers


  3. public boolean check(int num) {
    if (num 10);
    half = half / 2;
    int mod = 10;
    while (half > 0) {
    if (num / div != num % mod) {
    return false;
    }
    half--;
    }
    return true;
    }

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

  5. 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.

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

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

    }

    }

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

  9. 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.

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

  11. 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

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

  13. 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.

Leave a Comment