Given an integer x
, return true
if x
is a palindrome, and false
otherwise.
Example 1:
Input: x = 121 Output: true Explanation: 121 reads as 121 from left to right and from right to left.
Example 2:
Input: x = -121 Output: false Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: x = 10 Output: false Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Could you solve it without converting the integer to a string?
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to checking if a number is a palindrome involves directly comparing the number to its reversed version. We'll create the reversed version and then see if the two are identical. If they are, the number is a palindrome; if not, it isn't.
Here's how the algorithm would work step-by-step:
def is_palindrome_number_brute_force(number):
# Convert the number to a string for easy reversal.
number_string = str(number)
# Create the reversed version of the string
reversed_string = number_string[::-1]
# Comparing the original and reversed strings to determine palindromicity.
if number_string == reversed_string:
return True
else:
return False
To check if a number is a palindrome, we can avoid converting the number to a string and directly compare digits. We compare the first and last digits, then the second and second-to-last digits, and so on, moving inwards. If any pair doesn't match, it's not a palindrome.
Here's how the algorithm would work step-by-step:
def is_palindrome(number):
if number < 0:
return False
# Numbers ending in 0 (except 0 itself) are not palindromes
if number % 10 == 0 and number != 0:
return False
reversed_number = 0
while number > reversed_number:
reversed_number = reversed_number * 10 + number % 10
number //= 10
# For odd length palindromes, remove the middle digit
if number == reversed_number or number == reversed_number // 10:
return True
else:
return False
Case | How to Handle |
---|---|
Negative numbers | Negative numbers cannot be palindromes because of the leading minus sign, so return false. |
Zero | Zero is a palindrome, so return true. |
Single digit positive numbers | Single digit positive numbers are palindromes, so return true. |
Large positive integers that could lead to integer overflow when reversed | Employ techniques like comparing the first half of the number to the reversed second half, avoiding full reversal to prevent overflow. |
Integer close to the maximum integer value (INT_MAX) | Consider that reversing such numbers could lead to overflow and handle with similar approach as 'Large positive integers'. |
Numbers ending in zero (e.g., 10, 100) except 0 itself | Numbers ending in zero are not palindromes unless they are zero, so return false. |
Maximum integer value (INT_MAX) | Directly check if the reversed version of INT_MAX is equal to it, preventing any potential overflow during the calculation. |
Numbers with many leading zeros (e.g. 000121000) | Leading zeros are not usually represented in integer types and should not impact the palindrome property of the integer itself, but be mindful of implicit string conversion in some implementations. |