LeetCode-回文数

判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数

class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        string1 = str(x)
        list1 = list(string1)
        list2 = list(string1)
        list1.reverse()
        if list1 == list2:
            return True
        else:
            return False

因为list中有个reverse方法所以把数字转换成列表后,翻转列表,对比得出结论
list.reverse()方法是翻转内存地址中的列表,不返回新列表,所以要list2不能保存list1的地址,而是把list(string1)的内存地址赋值给list2

猜你喜欢

转载自blog.csdn.net/brook_/article/details/80189975