首页 > 代码库 > leetcode-happy number implemented in python

leetcode-happy number implemented in python

视频分析:
http://v.youku.com/v_show/id_XMTMyODkyNDA0MA==.html?from=y1.7-1.2



class Solution(object):
    def isHappy(self, n):
        if n==1:
            return True
        elif n==4:
            return False
        else:
            return self.isHappy(self.Happy(n))

    def Happy(self,n):
        z=0
        while n!=0:
            d=n%10
            z+=pow(d,2)
            n=(n-n%10)//10
        return z

    def HappySeq(self,start,cnt):
        tmp=start
        print (tmp,end=‘ ‘)
        for x in range(1, cnt):
            tmp=self.Happy(tmp)
            print (tmp,end=‘ ‘)
            if tmp==4 or tmp==1:
                break



<script type="text/javascript"> $(function () { $(‘pre.prettyprint code‘).each(function () { var lines = $(this).text().split(‘\n‘).length; var $numbering = $(‘
    ‘).addClass(‘pre-numbering‘).hide(); $(this).addClass(‘has-numbering‘).parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($(‘
  • ‘).text(i)); }; $numbering.fadeIn(1700); }); }); </script>

leetcode-happy number implemented in python