# Beat 97.87% Java Solution with Explanation

• The main thought is:
1.Get the number of digits, digitCount.
2.Compare digitCount/2 digits scanning from left to right and those from right to left one by one.

``````public boolean isPalindrome(int x) {
//negative integers can't be palindromes
if(x < 0)
return false;
//    	1.Get the number of digits--digitCount
int digitCount = (int) (Math.log10(x) + 1);
//      single digit is palindrome
if(digitCount == 1)
return true;
int x1 = x;
int divisor = (int)Math.pow(10, digitCount - 1);
//    	2.Compare digitCount/2 digits scanning from left to right and those from right to left one by one
for(int i = 0; i < digitCount / 2; i++, divisor /= 10){
int digit1 = x / divisor;//get digits from left to right
x = x - digit1 * divisor;
int digit2 = x1 % 10;//get digits from right to left
x1 /= 10;
if(digit1 != digit2)
return false;
}
return true;
}
``````

• you can use log10() to count the number of digits

• @taylorzhangyx Good idea!! I have optimized the code as you say.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.