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>
• alexwest11

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

• Marina

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

• Marina

return sum==x; is enough at least in java

• Marina

is this java?

• Kenneth Wong

it won’t work for integers with odd length

• Useful code. Thanks for sharing.

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

• Titan

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

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

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

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.