题解 | #丑数#

丑数

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]
全部评论

相关推荐

点赞 评论 收藏
分享
爱吃烤肠的牛油最喜欢...:50K是ssp了估计,ssp的人家多厉害都不用说,每年比例大概在百分之5左右
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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