码迷,mamicode.com
首页 > 编程语言 > 详细

Python算法题----Palindrome Number

时间:2016-01-20 15:47:38      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:python 算法 回文数字

Determine whether an integer is a palindrome. Do this without extra space.


class Solution(object):
    def numLen(self, n):
        i = 1
        while True:
            n /= 10
            if n > 0:
                i += 1
            else:
                break
        return i


    def pow(self, n):
        i = 1
        r = 1
        while i < n:
            r *= 10
            i += 1
        return r
    
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0:
            return False
        if x < 10:
            return True
        n = x
        nlen = self.numLen(n)
        t = self.pow(nlen)
        i = 0
        while i < (nlen / 2):
            if (x / t) % 10 != n % 10:
                return False
            n /= 10
            t /= 10
            i += 1
        return True


本文出自 “烛影摇红” 博客,请务必保留此出处http://gccmx.blog.51cto.com/479381/1736795

Python算法题----Palindrome Number

标签:python 算法 回文数字

原文地址:http://gccmx.blog.51cto.com/479381/1736795

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!