python判断回文数
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
#思路:先将整数转换为字符串,再将字符串翻转并与原字符串做比较
x = str(x)
return x == x[::-1]
代码简洁
第二个思路,尝试着不用字符串,将整数直接拆除一个数组,再比较这个数组是否“对称”
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
#思路二:将数字转换成数组
#负数肯定不是回文数
if x < 0 :
return False
elif x <= 9:
return True
, 相关下载链接:
https://download.csdn.net/download/u014481728/88506158?utm_source=bbsseo