Leetcode9.回文数(简单)Python

Leetcode9.回文数(简单)Python_第1张图片

小的注意点

1.注意最特殊的情况比如0

2.如果只是pos_x=pos_x/10,结果是12.1=121/10,所以int(pos_x/10)pos_x//10

3.return的True和False要大写

4.最后是a和x比较

class Solution(object):
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        pos_x=abs(x)
        a=0
        while(pos_x!=0):
            b=pos_x%10
            a=b+10*a
            pos_x=int(pos_x/10)
        if a==x and x>=0:
            return True
        else:
            return False

while的过程演示

Leetcode9.回文数(简单)Python_第2张图片

 知识点

python中的 /和// 

“/”是传统的除法,3/2=1.5

“//”:3//2=1,也可int(3/2)=1

你可能感兴趣的:(Leetcode刷题,leetcode,c++,算法)