Question:
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.
Answer:
The intuition behind this code is to reverse the entire input number and check if the reversed number is equal to the original number. If they are the same, then the number is a palindrome.
Approach 1: Reversing the Entire Number
1. We begin by performing an initial check. If the input number x is negative, it cannot be a palindrome since palindromes are typically defined for positive numbers. In such cases, we immediately return false.
2. We initialize two variables:
- reversed: This variable will store the reversed value of the number x.
- temp: This variable is a temporary placeholder to manipulate the input number without modifying the original value.
3. We enter a loop that continues until temp becomes zero:
- Inside the loop, we extract the last digit of temp using the modulo operator % and store it in the digit variable.
- To reverse the number, we multiply the current value of reversed by 10 and add the extracted digit.
- We then divide temp by 10 to remove the last digit and move on to the next iteration.
4. Once the loop is completed, we have reversed the entire number. Now, we compare the reversed value reversed with the original input value x.
- If they are equal, it means the number is a palindrome, so we return true.
- If they are not equal, it means the number is not a palindrome, so we return false.
The code uses a long long data type for the reversed variable to handle potential overflow in case of large input numbers.
class Solution: def isPalindrome(self, x: int) -> bool: if x < 0: return False
reversed_num = 0 temp = x
while temp != 0: digit = temp % 10 reversed_num = reversed_num * 10 + digit temp //= 10
return reversed_num == x
Approach 2: Reversing Half of the Number
- If the input number x is negative, it cannot be a palindrome since palindromes are typically defined for positive numbers. In such cases, we immediately return false.
- If x is non-zero and ends with a zero, it cannot be a palindrome because leading zeros are not allowed in palindromes. We return false for such cases.
- reversed: This variable will store the reversed second half of the digits of the number.
- temp: This variable is a temporary placeholder to manipulate the input number without modifying the original value.
- Inside the loop, we extract the last digit of x using the modulo operator % and add it to the reversed variable after multiplying it by 10 (shifting the existing digits to the left).
- We then divide x by 10 to remove the last digit and move towards the center of the number.
- For an even number of digits, if x is equal to reversed, then the number is a palindrome. We return true.
- For an odd number of digits, if x is equal to reversed / 10 (ignoring the middle digit), then the number is a palindrome. We return true.
- If none of the above conditions are met, it means the number is not a palindrome, so we return false.
class Solution: def isPalindrome(self, x: int) -> bool: if x < 0 or (x != 0 and x % 10 == 0): return False
reversed_num = 0 original = x
while x > reversed_num: reversed_num = reversed_num * 10 + x % 10 x //= 10
return x == reversed_num or x == reversed_num // 10
0 Comments