[leetcode:python]9.Palindrom Number

来源:互联网 发布:booking.it 编辑:程序博客网 时间:2024/06/06 00:39

题目:回文数
Determine whether an integer is a palindrome. Do this without extra space.
题意:判断一个数是否是回文数,不使用额外空间。

方法一:性能209ms

class Solution(object):    def isPalindrome(self, x):        """        :type x: int        :rtype: bool        """        y = x        if x < 0:            return False        ret = 0           while x != 0:            ret = ret*10 + x%10            x /=10        return ret == y

方法二:性能185ms

class Solution(object):    def isPalindrome(self, x):        """        :type x: int        :rtype: bool        """        return str(x)==str(x)[::-1]

这里的[::]可以起到reverse的作用
如:

list = [1,2,3,4,5,6]list[::-1] = [6,5,4,3,2,1]list[::-2] = [6,4,2]
0 0