Given an integer x, return true* if x is a palindrome, and false otherwise.
-231 <= x <= 231 - 1Could you solve it without converting the integer to a string?
We need time to reverse the integer, where n is the number of digits in the integer.
We need space to store the reversed integer as a string.
The idea is to reverse the integer and compare it with the original one. If they are the same, then the integer is a palindrome.
We need time to compare the characters from both ends, where n is the number of digits in the integer.
We only need space to store the pointers and the integer itself.
The idea is to use two pointers to compare characters from both ends of the integer. We start with the left pointer at the beginning of the integer and the right pointer at the end of the integer. We compare the characters at both pointers, if they are not the same, then the integer is not a palindrome. If they are the same, we move the left pointer to the right and the right pointer to the left until they meet in the middle.
We need time to reverse the integer, where n is the number of digits in the integer.
We only need space to store the reversed integer and the original integer itself.
The idea is to reverse the entire integer and compare it with the original one. We can use a while loop to reverse the digits of the integer. We get the last digit of the integer and add it to the reversed number, then we remove the last digit from the integer. After we have reversed the entire integer, we check if it is equal to the original integer.
We need ) time to reverse half of the integer, where n is the number of digits in the integer.
We only need space to store the reversed half and the integer itself.
The idea is to reverse half of the integer and compare it with the other half. We only need to reverse half of the integer because if the integer is a palindrome, then the first half should be the same as the second half. We can use a while loop to reverse the digits of the integer until we have reversed half of the digits. If the integer has an odd number of digits, we can get rid of the middle digit by reversed_half // 10.
We need ) time to compare the leading and trailing digits, where n is the number of digits in the integer.
We only need space to store the divisor and the integer itself.
The idea is to compare the leading and trailing digits of the integer. We can find the largest divisor to extract the leading digit. We then compare the leading and trailing digits, if they are not the same, then the integer is not a palindrome. If they are the same, we remove the leading and trailing digits and repeat the process until we have compared all digits.
| Approach | Rating | Time Complexity | Space Complexity | Advantages | Disadvantages |
|---|---|---|---|---|---|
| Reverse and Compare string | Simple to write and understand | ⚠️ Requires extra space for the string | |||
| Two Pointers | Minimal space usage by comparing characters | ⚠️ Still requires converting the integer to a string | |||
| Reverse Entire Number | Solves mathematically without string conversion | Potential for integer overflow when reversing | |||
| Reverse Half of the Number | ) | Avoids overflow by reversing only half the digits | Slightly more complex logic for odd-length numbers | ||
| Leading–Trailing Digit Comparison | ) | Compares digits directly from both ends | Uses extra division operations |
The optimal approach for this problem is Solution 4: Reverse Half of the Number, which achieves ) time complexity and space complexity. By only reversing the second half of the integer and comparing it to the first half, this method avoids both string allocation and potential integer overflow issues.
class Solution: def isPalindrome(self, x: int) -> bool: # reverse the integer and compare with the original one return str(x) == str(x)[::-1]class Solution: def isPalindrome(self, x: int) -> bool: # edge cases: negative numbers and numbers that end with 0 (except for 0 itself) cannot be palindromes if x < 0 or (x % 10 == 0 and x != 0): return False
# initialize two pointers to compare characters from both ends left, right = 0, len(str(x)) - 1
# compare the characters from both ends until they meet in the middle while left < right: if str(x)[left] != str(x)[right]: return False
left += 1 right -= 1
return Trueclass Solution: def isPalindrome(self, x: int) -> bool: # edge cases: negative numbers and numbers that end with 0 (except for 0 itself) cannot be palindromes if x < 0 or (x % 10 == 0 and x != 0): return False
revered_x = 0 original_x = x
while x > 0: # get the last digit of x and add it to the reversed number revered_x = revered_x * 10 + (x % 10) # remove the last digit from x x //= 10
# check if the original number is equal to the reversed number return original_x == revered_xclass Solution: def isPalindrome(self, x: int) -> bool: # edge cases: negative numbers and numbers that end with 0 (except for 0 itself) cannot be palindromes if x < 0 or (x % 10 == 0 and x != 0): return False
reversed_half = 0
# we only need to reverse half of the integer, because if the integer is a palindrome, then the first half should be the same as the second half while x > reversed_half: reversed_half = reversed_half * 10 + x % 10 x //= 10
# if the integer has an odd number of digits, we can get rid of the middle digit by reversed_half // 10 return x == reversed_half or x == reversed_half // 10