Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

只包含2、3、5中的1个或多个因子的数称为丑数,要求按从小到大的顺序找到第n个丑数 #135

Open
Sogrey opened this issue Feb 22, 2020 · 0 comments
Labels
Projects

Comments

@Sogrey
Copy link
Owner

Sogrey commented Feb 22, 2020

只包含2、3、5中的1个或多个因子的数称为丑数。

6 是丑数

14 =2*7 不是丑数

下一个丑数必定是数组中的某一个丑数A * 2、B * 3、C*5中最小的值

M2, 之前的丑数2都小于当前最大的丑数
之后的丑数
2都大于当前最大的丑数

M3、M5

def getUglyNumber(index):
    if index < 1:
        return 0
    res = [1]
    t2 = t3 = t5 = 0
    nextdex = 1
    while nextdex < index:
        minNum = min(res[t2] * 2, res[t3] * 3, res[t5] * 5)
        res.append(minNum)

        while res[t2] * 2 <= minNum:
            t2 += 1
        while res[t3] * 2 <= minNum:
            t3 += 1
        while res[t5] * 2 <= minNum:
            t5 += 1
        nextdex += 1
    return res[nextdex - 1]
print(getUglyNumber(100))  ## 633825300114114700748351602688

按顺序把每个丑数放在数组中,求下一个丑数下一个丑数必定是数组中的某一个丑数A * 2, B * 3, C * 5 的中的最小值。

@Sogrey Sogrey added the 算法 label Feb 22, 2020
@Sogrey Sogrey added this to 算法 in Python QAs Feb 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

1 participant