To solve this problem, we need to determine if a given integer is a palindrome. A palindrome number reads the same forwards and backwards. The solution should efficiently check this property without converting the number to a string (for optimal performance) and handle edge cases appropriately.
Approach
The key insight to solve this problem efficiently is to reverse half of the number and compare it with the remaining half. This approach avoids potential overflow issues (though not a problem in Python, it's a good practice) and reduces the number of operations needed. Here are the detailed steps:
-
Edge Cases Handling:
- Negative numbers are not palindromes (due to the minus sign).
- Numbers ending in 0 (except 0 itself) are not palindromes (since reversing them would start with 0, which is not valid).
-
Reverse Half the Number:
- Initialize a variable to store the reversed half of the number.
- Loop until the original number becomes less than or equal to the reversed half:
- Extract the last digit of the original number and add it to the reversed half.
- Remove the last digit from the original number.
-
Check Palindrome:
- If the length of the original number is even: The original half should equal the reversed half.
- If the length is odd: The reversed half (after removing the middle digit) should equal the original half.
Solution Code
def isPalindrome(num):
# Handle edge cases: negative numbers or numbers ending with 0 (except 0)
if num < 0 or (num % 10 == 0 and num != 0):
return False
reversed_half = 0
while num > reversed_half:
# Add the last digit of num to reversed_half
reversed_half = reversed_half * 10 + num % 10
# Remove the last digit from num
num = num // 10
# Check if num equals reversed_half (even length) or reversed_half//10 (odd length)
return num == reversed_half or num == reversed_half // 10
Explanation
Let’s break down the solution with examples:
-
Example 1: 121:
- After loop:
num = 1,reversed_half = 12. - Check:
1 == (12 //10) → True(since length is odd).
- After loop:
-
Example 2: 1331:
- After loop:
num =13,reversed_half=13. - Check:
13 == 13 → True(even length).
- After loop:
-
Example3: -121:
- Edge case: returns
Falseimmediately.
- Edge case: returns
-
Example4:120:
- Edge case: ends with 0 and not zero → returns
False.
- Edge case: ends with 0 and not zero → returns
-
Example5:0:
- Loop doesn’t run →
num == reversed_half (0 == 0 → True).
- Loop doesn’t run →
This approach runs in O(log₁₀ n) time (since we process half the digits of the number) and uses O(1) space (no extra data structures), making it optimal for large numbers.
Answer: The function isPalindrome as defined above correctly checks if a number is a palindrome. For example, isPalindrome(121) returns True, isPalindrome(-121) returns False, etc. The final code is as provided.
def isPalindrome(num):
if num < 0 or (num % 10 == 0 and num != 0):
return False
reversed_half = 0
while num > reversed_half:
reversed_half = reversed_half * 10 + num % 10
num = num // 10
return num == reversed_half or num == reversed_half // 10


作者声明:本文包含人工智能生成内容。