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.
Constraints:
-231 <= x <= 231 - 1
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 strategy for checking if a number is a palindrome involves directly comparing the original number to its reversed version. We create the reversed version, and then check if they are identical. If they are, the number is a palindrome.
Here's how the algorithm would work step-by-step:
def is_palindrome_brute_force(number_to_check):
string_representation = str(number_to_check)
reversed_string = string_representation[::-1]
# Compare the original and reversed strings to check
# if the number is a palindrome.
if string_representation == reversed_string:
return True
else:
return False
Determining if a number is a palindrome can be done cleverly without reversing the entire number. The trick is to compare only half of the number to its reversed counterpart.
Here's how the algorithm would work step-by-step:
def is_palindrome(number_to_check):
# Handle negative numbers and numbers ending in 0
if number_to_check < 0 or (number_to_check % 10 == 0 and number_to_check != 0):
return False
reversed_second_half = 0
while number_to_check > reversed_second_half:
reversed_second_half = (reversed_second_half * 10) + (number_to_check % 10)
number_to_check //= 10
# Compare first half to reversed second half
# For odd length palindromes, account for middle digit
if number_to_check == reversed_second_half or number_to_check == reversed_second_half // 10:
return True
else:
return False
Case | How to Handle |
---|---|
Negative numbers | Negative numbers cannot be palindromes because of the leading minus sign; return false immediately. |
Single-digit numbers | Single-digit numbers are always palindromes; return true immediately. |
Numbers ending in zero (excluding zero itself) | If a number ends in zero and is not zero itself, the reversed number will not end in zero, so it's not a palindrome; return false immediately. |
Integer overflow during reversal | Check if the reversed number exceeds Integer.MAX_VALUE / Integer.MIN_VALUE during the reversal process and return false if overflow is detected. |
Zero | Zero is a palindrome; return true. |
Large positive numbers | The solution should efficiently handle large numbers without causing performance issues or stack overflow using iterative rather than recursive approach. |
Maximum Integer Value (Integer.MAX_VALUE) | The solution should correctly classify Integer.MAX_VALUE as a non-palindrome |
Numbers very close to integer limits | The solution should correctly identify palindromes and non-palindromes that are close to Integer.MAX_VALUE or Integer.MIN_VALUE. |