[LeetCode]264. Ugly Number II

LeetCode 264. Ugly Number II(丑数II) 题目: 思路1(超时): 这是最初的思路,因为丑数可以被2和3还有5整除直到为1,所以可以从1开始逐个去找并累加,分别用判断能否被2整除,如果可以就除2,再次重复这样的步骤,直到不能被2整除,再分别拿3和5做同样的事情。最终剩下的是1就说明这是一个丑数,用这样的方式去找第n个就可以了。这种方式很好理解,但是容易超时。因为它做了一些重复的事情,比如每个数都要追溯到不能被2和3还有5整除。 思路2(... 阅读详情

[LeetCode]264. Ugly Number II

题目描述

这里写图片描述

思路

动态规划,题目313的简单版
题目313看这里
[LeetCode]313. Super Ugly Number

代码

#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

class Solution {
public:
    int nthUglyNumber(int n) {
        vector<int> primes = { 2,3,5 }, indexArr = { 0,0,0 }, uglyArr(n, INT_MAX);
        uglyArr[0] = 1;
        for (int i = 1; i < n; ++i) {
            for (int index = 0; index < 3; index++) uglyArr[i] = min(uglyArr[i], uglyArr[indexArr[index]] * primes[index]);
            for (int index = 0; index < 3; index++) indexArr[index] += (uglyArr[i] == uglyArr[indexArr[index]] * primes[index]);
        }
        return uglyArr[n - 1];
    }
};

int main() {
    Solution s;
    cout << s.nthUglyNumber(10) << endl;

    system("pause");
    return 0;
}
C++保姆级教程(十五):项目实战 本文详细介绍了使用C++开发图书管理系统的全过程。系统采用模块化设计,包含图书管理、读者管理、借阅管理等核心功能模块,通过文件操作实现数据持久化。文章从项目开发流程、系统设计、核心功能实现、测试调试到优化扩展进行了全面讲解,重点展示了面向对象编程思想、文件操作、异常处理等关键技术的应用。项目采用规范的代码结构和命名规范,适合初学者学习完整的C++项目开发流程。通过这个实战项目,读者可以掌握如何将零散的C++知识点串联起来,构建一个功能完善、结构清晰的实用系统。 阅读详情

相关推荐

东华大学2020考研计算机OJ题目解答分享——进阶篇(4)

4 一个月的天数 作者: Turbo时间限制: 1S章节: 分支结构 问题描述 : 输入年和月,输出该月有几天。 输入说明 : 输入两个整数,中间以空格分隔,第一个整数表示年,第二个整数表示月。 输出说明 : 输出该年该月的天数,输出时,行首与行尾均无空格,仅输出一个整数。 输入范例 : 2000 2 输出范例 : 29 解答思路: 闰年和质数出题一样频繁,简单必会 AC代码 #include...

qq_38344326的博客 479

Leetcode 264. Ugly Number II

Leetcode 264. Ugly Number II @(LeetCode)[LeetCode | Algorithm | DP] Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. ...

TooSchoolForCool 285

【STM32-DSP库的使用】基于Keil5 + STM32CubeMX + CMSIS-DSP 手动添加、库添加方式

CMSIS-DSP库在工程中应用遇到的所有问题进行归纳终结

洋洋的博客 1万+

LeetCode: 264. Ugly Number II

LeetCode: 264. Ugly Number II 题目描述 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. Example: Input: n = 10 Output: 12 Explanati...

杨领well的专栏 264

WSL基础环境配置方案

通过以上步骤,您已经在 Windows 的 WSL 环境中成功搭建了一个完整的基础开发环境,包含 Docker、Kubernetes、Helm、kubectl、yq、Git 等工具。这套环境特别适用于在国内使用阿里云镜像服务部署和管理 Tekton,提升了开发和运维的效率和稳定性。

落魄书生的职业学习生涯 8882

基于STM32L4XX、HAL库的IP101GRI以太网收发器(PHY)驱动程序设计

define IP101GRI_PHY_ADDRESS 0x01 // 根据硬件设计设置PHY地址。#define IP101GRI_PHYSTS 0x10 // PHY状态寄存器。#define IP101GRI_PHYINIR 0x12 // PHY中断寄存器。#define IP101GRI_PHYCR 0x14 // PHY控制寄存器。#define IP101GRI_PHYID2 0x03 // PHY标识符2。

zhusl6688的博客 733

leetcode 264. Ugly Number II

关键是理解如何生成新的丑数。这道题和经典的斐波那契数列问题其实是一样的。求第n个数,需要用第n个数前面的数来求。不同的是,斐波那契数列不会重复。而本题的丑数,会重复出现。

weixin_41554427的博客 354

如何借助Gemini 3 Pro完成一篇文献精读?(劝收藏)

金句必须是原文的完整句子,并附上该句子在文中的大致位置。你是一名资深科研文献评估专家,请根据我提供的文献信息(标题、作者、期刊、时间、引用、摘要、结论、图表)依次输出:①影响力评估:按期刊、作者、时间、引用、问题重要性给出1–10分及简述;用上述方法再结合Gemini 3 Pro的辅助,原本需要3小时才能读完一篇文献,现在只需50分钟,不管是顶刊论文还是领域内冷门论文,都可以用这套方法,另外七哥设计的高阶精读提示词,需根据自己的文献内容做调整,希望这篇文章的方法能帮你高效精读文献,掌握文献的精华内容。

智能AI让学术科研更容易 1313

leetcode 264: Ugly Number II

Ugly Number II Total Accepted: 2920 Total Submissions: 15174 Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For

西施豆腐渣 6179

基于STM32、HAL库的ICP-20100气压传感器 驱动程序设计

它具有低功耗、高精度、快速响应等特点,非常适合物联网、可穿戴设备和无人机等应用。#define ICP20100_FIFO_CTRL 0x09 // FIFO控制寄存器。#define ICP20100_FIFO_DATA 0x0A // FIFO数据寄存器。#define ICP20100_FIFO_STS 0x0B // FIFO状态寄存器。#define ICP20100_FIFO_WTM 0x15 // FIFO水印设置。

corlin6688的博客 608

华为OD机试 - 小火车最多人时所在园区站点(Java 2024 E卷 100分)

刷的越多,抽中的概率越大,私信哪吒,备注华为OD,加入华为OD刷题交流群,每一题都有详细的答题思路、详细的代码注释、发现新题目,随时更新,全天CSDN在线答疑。

学Java,找哪吒 999

[Leetcode 264] Ugly Number II

Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 

coder 进阶的专栏 2874

使用 MATLAB/Simulink + Simscape Electrical 来构建一个CCHP微电网与新能源协同运行的模型

随着能源需求的增长和对环境保护要求的提高,冷热电联供(Combined Cooling, Heating and Power, CCHP)系统作为一种高效的分布式能源解决方案,越来越受到关注。CCHP系统通过利用一次能源同时产生电力、制冷和供热,实现了能源的梯级利用,提高了能源使用效率。结合太阳能、风能等可再生能源,可以进一步增强系统的灵活性和环保性。本文将详细介绍如何使用来构建一个CCHP微电网与新能源协同运行的模型,并通过仿真分析其性能表现。本文介绍了如何使用构建一个。

xiaoheshang_123的博客 145

leetcode 264. Ugly Number II

经典的找第几个丑数是啥 大一小孩都会 https://leetcode.com/problems/ugly-number-ii/ Write a program to find then-th ugly number. Ugly numbers arepositive numberswhose prime factors only include2, 3, 5. Example...

MissZhou要努力 265

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

【Leetcode】264. Ugly Number II

题目地址: https://leetcode.com/problems/ugly-number-ii/ 一个正整数是丑数,含义是它的素因子只有2,3,52,3,52,3,5。规定111是第111个丑数,问第nnn个丑数是几。 思路是多路归并。设丑数集合为SSS,考虑三个序列:S2=[2×1,2×2,2×3,...]=2SS3=[3×1,3×2,3×3,...]=3SS5=[5×1,5×2,5×3,...]=5SS_2=[2\times 1,2\times 2,2\times 3,...]=2S\\S_3=[

数学、算法爱好者的博客 323

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

[理解leetcode解法]264. Ugly Number II

[理解leetcode解法]264. Ugly Number II

存在感极其低下的弟弟 631

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

leetcode263_264:Ugly Number & Ugly Number II

Ugly NumberWrite a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly

devbarry的专栏 758

Leetcode 264(Ugly Number II)

QuestionWrite a program to find the nthn^{th} ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the

senliuy的专栏 894

[leetcode] 264.Ugly Number II

题目: Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10

u014673347的专栏 760

LeetCode 264 Ugly Number II

题目描述Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10

Yano_nankai的博客 650

LeetCode 264. Ugly Number II

Write a program to find then-th ugly number. Ugly numbers arepositive numberswhose prime factors only include2, 3, 5. Example: Input: n = 10 Output: 12 Explanation: 1, 2, 3, 4, 5, 6, 8,...

qq_39638957的博客 178

leetcode264-Ugly Number II

问题题目:[leetcode-264]思路只想到了朴素的思路,果然TLE。 参考了这篇链接。[ugly number ii] 其实,它的思路也很明显。和素数筛法差不多,这个可以叫做丑数筛法吧。 具体思路是这样,因为任何一个丑数的素因子只能是2,3,5.所以,可以进一步知道,任何一个大的丑数,都是由小的丑数×2,3,5生成。所以,所有的丑数都是从1开始,然后乘以2,3,5生成。这个不是难点,难点

Kang_TJU的博客 471

[leetcode] 264. Ugly Number II 解题报告

题目链接:https://leetcode.com/problems/ugly-number-ii/ Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2

小榕流光的专栏 2675

Leetcode 264. Ugly Number II

问题描述 Write a program to find the n-th ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the

u010370157的博客 250

leetcode 264. Ugly Number II 题解

原题: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of t

yoyo__123的博客 376
上一篇: [LeetCode]313. Super Ugly Number
下一篇: [LeetCode]746. Min Cost Climbing Stairs
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值