You are given an integer x, return true if x is a palindrome, and false otherwise.
Note: An integer is a palindrome when it reads the same forward and backward.
For example, 111 and 121 are palindromes while 123 is not.
Example 1:
Input: x = 1
Output: trueExample 2:
Input: x = -1
Output: falseConstraints:
-(2^31) <= x <= ((2^31) - 1)Follow up: Could you solve it without converting the integer to a string?
The most straightforward approach is to convert the integer to a string and check if the string is a palindrome by reversing it.
If the original string equals its reversed version, the number is a palindrome.
true if they are equal, false otherwise.Where is the number of digits in the given integer.
Instead of creating a reversed copy of the entire string, we can compare characters from both ends moving toward the center. This avoids the extra space needed for the reversed string.
We only need to check half the string since we compare characters in pairs.
i at the start and j at the end.i < n/2, compare s[i] with s[n - i - 1]. If they differ, return false.true if all comparisons pass.Where is the number of digits in the given integer.
Without using string conversion, we can reverse the entire integer mathematically and compare it to the original. If they are equal, the number is a palindrome.
Negative numbers cannot be palindromes because of the negative sign. We build the reversed number digit by digit using modulo and division operations.
false immediately.rev = 0 and num = x.num > 0:num % 10.rev by computing rev = rev * 10 + digit.num using integer division.rev with the original x and return true if equal.Where is the number of digits in the given integer.
We can compare digits from both ends without converting to a string or reversing the entire number. The idea is to extract the leftmost and rightmost digits and compare them.
We use a divisor to extract the leftmost digit and modulo to extract the rightmost digit, then shrink the number from both ends.
false.div such that x / div gives the first digit. This is the largest power of 10 less than or equal to x.x > 0:x / div) with the last digit (x % 10). If they differ, return false.x = (x % div) / 10.div /= 100 (since we removed two digits).true if all comparisons pass.Where is the number of digits in the given integer.
Reversing the entire number risks integer overflow. A clever optimization is to reverse only the second half of the number and compare it to the first half.
We keep extracting digits from the end and building the reversed half until the reversed half is greater than or equal to the remaining number. At that point, we have processed half (or just past half) of the digits.
0 (except 0 itself) are not palindromes.rev = 0.x > rev:x and append it to rev.x.x == rev (even length) or x == rev / 10 (odd length, where the middle digit is in rev).true if either condition holds.Where is the number of digits in the given integer.
Negative numbers have a minus sign at the front, so they can never be palindromes (e.g., -121 reads as 121- backwards). Failing to handle negative numbers as an early return case leads to incorrect results or issues with modulo operations on negative values.
Numbers ending in zero (except zero itself) cannot be palindromes because leading zeros are not allowed in integers. For example, 10 would need to be 01 reversed, which is not a valid representation. The half-reversal approach fails on these cases without explicit handling, as the loop condition x > rev may never be satisfied correctly.