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 - 1When 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:
To check if a number reads the same forwards and backwards using the brute force approach, we can convert the number into text form and look at it from both directions at once. By systematically comparing the characters from opposite ends, we can determine if they match completely.
Here's how the algorithm would work step-by-step:
def is_palindrome(input_number):
# Convert the number to text so we can easily access characters from both ends
string_representation = str(input_number)
left_pointer = 0
right_pointer = len(string_representation) - 1
# Compare characters inward until our pointers meet or cross each other
while left_pointer < right_pointer:
if string_representation[left_pointer] != string_representation[right_pointer]:
# Immediate failure means we stop early because it is not a palindrome
return False
left_pointer += 1
right_pointer -= 1
# Reaching this point means all character pairs matched successfully
return TrueTo determine if a number reads the same forwards and backwards, the most efficient strategy is to take the second half of the number and flip it around. By comparing this flipped version to the first half, we can instantly tell if the number is a palindrome without having to process every single digit.
Here's how the algorithm would work step-by-step:
def is_palindrome(candidate_number):
# Negative numbers and numbers ending in zero cannot be palindromes except zero itself.
if candidate_number < 0 or (candidate_number % 10 == 0 and candidate_number != 0):
return False
mirrored_version = 0
# We peel digits until the original number is less than or equal to the mirrored half.
while candidate_number > mirrored_version:
last_digit = candidate_number % 10
mirrored_version = (mirrored_version * 10) + last_digit
candidate_number = candidate_number // 10
# Check for both even and odd length numbers by comparing halves.
return candidate_number == mirrored_version or candidate_number == mirrored_version // 10| Case | How to Handle |
|---|---|
| Negative integers | Return false immediately because the negative sign at the front prevents it from ever matching the reversed number. |
| Single-digit numbers from zero to nine | Return true automatically since single-digit numbers always read the same forwards and backwards. |
| Numbers ending with zero except zero itself | Return false because no non-zero integer can start with a zero when reversed. |
| Even-length palindromes like 1221 | Compare the original left half with the fully reversed right half after half the iterations. |
| Odd-length palindromes like 12321 | Compare the left half with the right half after removing the middle digit by integer division by ten. |
| Maximum and minimum boundary integer values | Reversing only half of the digits completely avoids potential integer overflow issues. |
| Numbers with all identical digits like 1111 | Symmetric numeric construction naturally treats these as valid palindromes. |
| Zero input | Return true because zero is symmetrical and fits the base palindrome definition. |