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

你可能感兴趣的:(python,算法)