leetcode丑数II计算第n个丑数

给你一个整数 n ,请你找出并返回第 n 个 丑数 。

丑数 就是质因子只包含 2、3 和 5 的正整数。

示例 1:

输入:n = 10

输出:12

解释:[1, 2, 3, 4, 5, 6, 8, 9, 10, 12] 是由前 10 个丑数组成的序列。

示例 2:

输入:n = 1

输出:1

解释:1 通常被视为丑数。

提示:

1 <= n <= 1690

如果从1开始计算:

python 复制代码
class Solution:
    def nthUglyNumber(self, n: int) -> int:
        res = [1,1]
        n1=1
        n2=1
        n3=1
        for i in range(1,n):
            num1 = res[n1] *2
            num2 = res[n2] *3
            num3 = res[n3] * 5
            temp = min(num1, num2, num3)
            res.append(temp)
            if temp == num1:
                n1 = n1 + 1
            if temp == num2:
                n2 = n2 +1
            if temp == num3:
                n3 = n3 +1
        return res[n]
        
        

如果从0开始算

python 复制代码
class Solution:
    def nthUglyNumber(self, n: int) -> int:
        res = [1]
        n1=0
        n2=0
        n3=0
        for i in range(1,n):
            num1 = res[n1] *2
            num2 = res[n2] *3
            num3 = res[n3] * 5
            temp = min(num1, num2, num3)
            res.append(temp)
            if temp == num1:
                n1 = n1 + 1
            if temp == num2:
                n2 = n2 +1
            if temp == num3:
                n3 = n3 +1
        return res[-1]
        
        
相关推荐
code小毛孩1 小时前
leetcode hot100数组:缺失的第一个正数
数据结构·算法·leetcode
legendary_bruce7 小时前
【22-决策树】
算法·决策树·机器学习
独行soc7 小时前
2025年渗透测试面试题总结-18(题目+回答)
android·python·科技·面试·职场和发展·渗透测试
max5006008 小时前
基于桥梁三维模型的无人机检测路径规划系统设计与实现
前端·javascript·python·算法·无人机·easyui
快去睡觉~10 小时前
力扣400:第N位数字
数据结构·算法·leetcode
qqxhb11 小时前
零基础数据结构与算法——第七章:算法实践与工程应用-搜索引擎
算法·搜索引擎·tf-idf·倒排索引·pagerank·算法库
gzzeason12 小时前
LeetCode Hot100:递归穿透值传递问题
算法·leetcode·职场和发展
汤永红12 小时前
week1-[循环嵌套]画正方形
数据结构·c++·算法
pusue_the_sun12 小时前
数据结构——顺序表&&单链表oj详解
c语言·数据结构·算法·链表·顺序表
yi.Ist13 小时前
图论——Djikstra最短路
数据结构·学习·算法·图论·好难