You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
'''
|
|
|
|
|
判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。
|
|
|
|
|
示例 1:输入: 121 输出: true
|
|
|
|
|
示例 2:输入: -121 输出: false 解释: 从右向左读为 121- 。
|
|
|
|
|
示例 3:输入: 10 输出: false 解释: 从右向左读为 01 。
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
class Solution(object):
|
|
|
|
|
def _isPalindrome(self, x): # 列表法,用两个指针,一个从前,一个从后判断是否相同
|
|
|
|
|
"""
|
|
|
|
|
:type x: int
|
|
|
|
|
:rtype: bool
|
|
|
|
|
"""
|
|
|
|
|
str_x = list(str(x))
|
|
|
|
|
i , j = 0 , len(str_x) - 1
|
|
|
|
|
while i < j :
|
|
|
|
|
if str_x[i] != str_x[j]:
|
|
|
|
|
return False
|
|
|
|
|
i += 1
|
|
|
|
|
j -=1
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def isPalindrome(self, x: int): # 字符串法
|
|
|
|
|
if x < 0:
|
|
|
|
|
return False
|
|
|
|
|
t=str(x)
|
|
|
|
|
t =t[::-1] # 相当于t[-1:-len(t)-1:-1],从最后一个元素到第一个元素复制一遍,即倒序
|
|
|
|
|
t=int(t)
|
|
|
|
|
if x==t:
|
|
|
|
|
return True
|
|
|
|
|
else :
|
|
|
|
|
return False
|