题解 | #丑数#

丑数

http://www.nowcoder.com/practice/6aa9e04fc3794f68acf8778237ba065b

# -*- coding:utf-8 -*-
class Solution:
    def GetUglyNumber_Solution(self, index):
        # write code here
        if index <= 0:
            return 0
        uglyList = [1]
        p2, p3, p5 = 0, 0, 0
        for i in range(index-1):
            curr = [uglyList[p2]*2, uglyList[p3]*3, uglyList[p5]*5]

            newUgly = min(curr)
            uglyList.append(newUgly)
            if newUgly%2 == 0:
                p2 += 1
            if newUgly%3 == 0:
                p3 += 1
            if newUgly%5 == 0:
                p5 += 1
            print(curr, newUgly, uglyList)
        return uglyList[index-1]
全部评论

相关推荐

不愿透露姓名的神秘牛友
06-27 15:19
简历上能写3个月吗?
码农索隆:大胆写,主要你能把实习经历包装好,可以看一下我这篇帖子https://www.nowcoder.com/share/jump/4888395581180798063
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务