Input: 121
Output: true
Input: -121
Output: false
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
思路
这个题目比较简单,只需要根据回文数定义判断就好了。
另外,我们也可以用一种比较 Pythonic 的解决方案。
答案
# 定义方法classSolution:defisPalindrome(self,x:int) ->bool: s =str(x)for idx inrange(len(s) //2):if s[idx]!= s[-idx-1]:returnFalsereturnTrue# Pythonic 方法classSolution:defisPalindrome(self,x:int) ->bool:returnstr(x)[::-1] ==str(x)andnot x <0