题目
代码
执行用时:68 ms, 在所有 Python3 提交中击败了37.40% 的用户
内存消耗:14.8 MB, 在所有 Python3 提交中击败了92.63% 的用户
通过测试用例:11510 / 11510
class Solution:
def isPalindrome(self, x: int) -> bool:
s=str(x)
return s==s[::-1]
【方法2】
执行用时:88 ms, 在所有 Python3 提交中击败了6.56% 的用户
内存消耗:14.9 MB, 在所有 Python3 提交中击败了52.56% 的用户
通过测试用例:11510 / 11510
class Solution:
def isPalindrome(self, x: int) -> bool:
s=str(x)
left,right=0,len(s)-1
while left<=right:
if s[left]!=s[right]:
return False
left+=1
right-=1
return True
【方法3】翻转全部数字
执行用时:76 ms, 在所有 Python3 提交中击败了18.05% 的用户
内存消耗:14.9 MB, 在所有 Python3 提交中击败了78.31% 的用户
通过测试用例:11510 / 11510
class Solution:
def isPalindrome(self, x: int) -> bool:
if x<0:
return False
temp=0
original_x=x
while x!=0:
temp=temp*10+x%10
x//=10
return temp==original_x
【方法4】翻转一半数字
执行用时:64 ms, 在所有 Python3 提交中击败了51.87% 的用户
内存消耗:14.8 MB, 在所有 Python3 提交中击败了86.93% 的用户
通过测试用例:11510 / 11510
class Solution:
def isPalindrome(self, x: int) -> bool:
if x>=0 and x<10:
return True
if x<0 or x%10==0:
return False
temp=0
while x!=0:
temp=temp*10+x%10
x//=10
if temp==x or temp==(x//10):
return True
return False