首页 > 其他 > 详细

【剑指offer】丑数

时间:2018-08-10 17:43:41      阅读:247      评论:0      收藏:0      [点我收藏+]

一、题目:

      把只包含质因子2、3和5的数称作丑数(Ugly Number)。例如6、8都是丑数,但14不是,因为它包含质因子7。 习惯上我们把1当做是第一个丑数。求按从小到大的顺序的第N个丑数。

二、思路:

   en.....自己想不出来,还是抄袭别人的吧。

   技术分享图片

技术分享图片

技术分享图片

三、代码:

 

# -*- coding:utf-8 -*-
class Solution:
    def GetUglyNumber_Solution(self, index):
        # write code here
        if index<7:return index
        res=[1]
        p1=p3=p5=0
        next_index=1
        while next_index<index:
            min_value=min(res[p1]*2,res[p3]*3,res[p5]*5)
            res.append(min_value)
            if min_value==res[p1]*2:p1+=1
            if min_value==res[p3]*3:p3+=1
            if min_value==res[p5]*5:p5+=1
            next_index+=1
        return res[index-1]

 

【剑指offer】丑数

原文:https://www.cnblogs.com/EstherLjy/p/9456437.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!