方法一:转化为字符串,然后反转 O(n)
class Solution:
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0:
return False
else:
x=str(x)
y=x[::-1]
if x==y:
return True
else:
return False
方法二:双端队列:转化为list,依次弹出首尾两个数 O(n^2)
class Solution:
def isPalindrome(self, x):
"""
:type x: int
:rtype: bool
"""
if x<0:
return False
else:
still_equal=True
x=list(str(x))
while len(x)>1 and still_equal:
left=x.pop(0)
right=x.pop()
if left != right:
still_equal=False
return still_equal