Allen
Allen

Reputation: 181

Determine if an int is a palindrome in Java?

I was working on a problem on LeetCode that asks the question: determine whether an integer is a palindrome without converting the integer into a string. I am able to come up with my own algorithm to determine a palindrome but couldn't come up with one without converting the integer to a string. So I ran into this code on the Internet:

public boolean isPalindrome(int x) {
    if (x < 0)
        return false;

    // initialize how many zeros
    int div = 1;
    while (x / div >= 10) {
        div = div * 10;
    }

    while (x != 0) {
        int left = x / div;
        int right = x % 10;

        if (left != right)
            return false;

        x = (x % div) / 10;
        div = div / 100;
    }   
    return true;
}

I step through the code several times to see what it was doing but I am having such a hard time understanding how this person was able to come up with this logic. Now, I know how division and modulo works. However, this part is fuzzy:

// initialize how many zeros
int div = 1;
while (x / div >= 10) {
    div = div * 10;
}

I believe this person is trying to determine the decimal place?

The next snippet part of their code, I'm completely baffled as to how they came up with this. I mean ... is this some simple math trick that I'm obviously not coming up with an explanation? In simple terms, what are they doing here? How did they decide on the divisor of 10 and 100?

x = (x % div) / 10;
div = div / 100;

I appreciate any explanation to help me understand. Thanks in Advance.

v/r,

Allen

Upvotes: 1

Views: 567

Answers (2)

duyuanchao
duyuanchao

Reputation: 4303

private static boolean isPalindrome(int number) {
    int temp = number;
    int result = 0;
    while (temp != 0) {
        result = result * 10 + temp % 10;
        temp /= 10;
    }
    return number == result;
}

Upvotes: 1

Prakash Bansal
Prakash Bansal

Reputation: 156

Let's take x=12321

Then

int div = 1;
while (x / div >= 10) {
    div = div * 10;
}

above part makes div = 10000,

then this part makes left = 1 and right = 1, and then left and right are compared

int left = x / div;
int right = x % 10;

Then, as we move through this part

x = (x % div) / 10;
div = div / 100;

this occurs

x = (12321%10000)/10 = (2321)/10 = 232

They modulo with div, to get rid of the first digit and then they divide by 10 to remove the last digit. The first and the last digits are removed because they have already been compared. The div is divided by 100, because we remove 2 digits from 'x' at a time...the first digit and the last digit.

now, x = 232

This part makes left = 2 and right = 2, and again left and right are compared

int left = x / div;
int right = x % 10;

then, as we move through this part

x = (x % div) / 10;
div = div / 100;

this occurs

x = (232%100)/10 = (32)/10 = 3

After this, left = 3 and right = 3, they match, 'true' is returned and the loop ends.

Upvotes: 2

Related Questions