class Solution:
def isPalindrome(self,x:int) -> bool:
y = str(x)
a = []
b = []
for i in range(len(y) - 1):
a.insert(i,y[i])
for j in range(len(y) - 1):
b.insert(j,y[len(y) - 1 - j])
if a == b:
return True
else:
return False
class Solution2:
def isPalindrome(self,x:int) ->bool:
a = str(x)
b = str(x)[::-1] #字符串逆序 [:]表示取全部 [::-1]表逆序 [::2]表示每隔1个取1个 左闭右开
return a == b
# return str(x) == str(x)[::-1]
class Solution3:
def isPalindrome(self,x:int) ->bool:
if x < 0 or (x%10 == 0 and x!= 0):
return False
rem = 0
while x > rem:
rem = rem*10 + x%10
x= x//10
return x == rem or x == rem//10
if __name__ == '__main__':
a = Solution3()
b = a.isPalindrome(131)
print(b)
leecode02 回文数
最新推荐文章于 2024-07-25 21:13:04 发布