剑指offer:丑数(Python)

题目描述

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

解题思路

如果p是丑数,那么p=2^x * 3^y * 5^z。那么只要赋予x,y,z不同的值就能得到不同的丑数。

Python代码

def GetUglyNumber_Solution(self, index):
    if index < 1:
        return 0
    res = [1]
    t2,t3,t5 = 0,0,0
    while len(res) < index:
        minNum = (min(res[t2]*2, res[t3]*3, res[t5]*5))
        if minNum > res[-1]: res.append(minNum)
        if (res[-1] == res[t2]*2): t2 += 1
        elif (res[-1] == res[t3]*3): t3 += 1
        else: t5 += 1
    return res[-1]

猜你喜欢

转载自blog.csdn.net/u010005281/article/details/80085543