python判断回文数的两种方法--字符串和数字

1.转字符串

class Solution:
    def isPalindrome(self, x: int) -> bool:
        x = str(x)
        lenth = len(x)
        for i in range(0,lenth//2):
            if x[i] != x[lenth-i-1]:
                return False
        return True

2.数字

class Solution:
    def isPalindrome(self, x: int) -> bool:
       if x<0:
           return False
       ls = []
       while x:
            i = x % 10
            x = x //10
            ls.append(i)
       lenth = len(ls)
       for i in range(0, lenth // 2):
            if ls[i] != ls[lenth - i - 1]:
                return False
       return True
发布了23 篇原创文章 · 获赞 9 · 访问量 4210

猜你喜欢

转载自blog.csdn.net/qq1225598165/article/details/101721976