LeetCode力扣09:回文数

回文数

LeetCode力扣09:回文数_第1张图片

代码实现

  • 代码1是使用将整形数字倒置来进行对比,最后得出答案
class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x<0:
            return False
        res=0
        old=x
        while x:
            a=x%10
            res=res*10+a
            x//=10
        return res==old
  • 代码2是将整形数字强类型转换成字符串类型,再利用python中的切片从而达到字符串倒置的效果,最后直接进行比较就行
class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        return str(x)==str(x)[::-1]

你可能感兴趣的:(leetcode,算法,职场和发展)