LeetCode — Palindrome Number(Easy)

Katherine
1 min readFeb 23, 2021

Palindrome Number(Easy)

Given an integer x, return true if x is palindrome integer.

An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not.

Example 1:

Input: x = 121
Output: true

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.

My Solution

class Solution(object):
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x < 0:
return False

x = str(x)

for i in range(len(x)):
if x[i] != x[len(x)-1-i]:
return False

return True

Result

Runtime: 56 ms, faster than 60.83% of Python online submissions for Palindrome Number.

Memory Usage: 13.5 MB, less than 39.92% of Python online submissions for Palindrome Number.

Follow up: Could you solve it without converting the integer to a string?

Todo

--

--