Leetcode Day 2: Palindrome Number

The problem is as follows:
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 = -12…


This content originally appeared on DEV Community and was authored by Simona Cancian

The problem is as follows:
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.

Here is how I solved it:

  • Let's convert the given integer into a string
  • In Python we can reverse a string by slicing: create a slice that starts at the end of the string and moves backwards. The slice statement [::-1] means start and end of the string and end at position 0, move with the step -1, which means one step backwards. https://www.w3schools.com/python/python_howto_reverse_string.asp
rev = str(x)[::-1]
  • Let's compare the reversed string to the given integer (again, convert it to string: we can only compare same datatypes). Return True if it matches, else False.
if rev == str(x):
    return True
return False

But hey, we can actually write just one line of code using ternary operator! To be honest, I am not sure if it's bad design, but it seemed more readable to me.
Here is the complete solution:

class Solution:
    def isPalindrome(self, x: int) -> bool:
        return True if str(x)[::-1] == str(x) else False


This content originally appeared on DEV Community and was authored by Simona Cancian


Print Share Comment Cite Upload Translate Updates
APA

Simona Cancian | Sciencx (2024-07-03T02:19:12+00:00) Leetcode Day 2: Palindrome Number. Retrieved from https://www.scien.cx/2024/07/03/leetcode-day-2-palindrome-number/

MLA
" » Leetcode Day 2: Palindrome Number." Simona Cancian | Sciencx - Wednesday July 3, 2024, https://www.scien.cx/2024/07/03/leetcode-day-2-palindrome-number/
HARVARD
Simona Cancian | Sciencx Wednesday July 3, 2024 » Leetcode Day 2: Palindrome Number., viewed ,<https://www.scien.cx/2024/07/03/leetcode-day-2-palindrome-number/>
VANCOUVER
Simona Cancian | Sciencx - » Leetcode Day 2: Palindrome Number. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/03/leetcode-day-2-palindrome-number/
CHICAGO
" » Leetcode Day 2: Palindrome Number." Simona Cancian | Sciencx - Accessed . https://www.scien.cx/2024/07/03/leetcode-day-2-palindrome-number/
IEEE
" » Leetcode Day 2: Palindrome Number." Simona Cancian | Sciencx [Online]. Available: https://www.scien.cx/2024/07/03/leetcode-day-2-palindrome-number/. [Accessed: ]
rf:citation
» Leetcode Day 2: Palindrome Number | Simona Cancian | Sciencx | https://www.scien.cx/2024/07/03/leetcode-day-2-palindrome-number/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.