[LeetCode]#9. Palindrome Number

Fatboy Slim
1 min readSep 2, 2020

--

Environment: Python 3.7

Key technique: abs

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Example 1:

Input: 121
Output: true

Analysis:

  1. If input <0; return False
  2. If input >0; input switch location such as “123” to "321"
  3. If input == switched input; return True

Solution:

class Solution:
def isPalindrome(self, x):
if x<0:
return False
s = int(str(abs(x))[::-1])
if(x == s):
return True

Submissions:

Reference:

https://leetcode.com/problems/palindrome-number/discuss/824071/Python-Simplest-using-string

--

--

Fatboy Slim
Fatboy Slim

Written by Fatboy Slim

Interesting in any computer science.

No responses yet