Leetcode264. Ugly Number II丑数2
编写一个程序,找出第 n 个丑数。
丑数就是只包含质因数 2, 3, 5 的正整数。
示例:
输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。
说明:
- 1 是丑数。
- n 不超过1690。
不用else if的原因是为了去重
class Solution {
public:int nthUglyNumber(int n) {int three = 0;int two = 0;int five = 0;vector<int> res(n, 0);res[0] = 1;for(int i = 1; i < n; i++){res[i] = min(res[two] * 2, min(res[three] * 3, res[five] * 5));if(res[i] == res[two] * 2){two++;}if(res[i] == res[three] * 3){three++;}if(res[i] == res[five] * 5){five++;}}return res[n - 1];}
};
转载于:https://www.cnblogs.com/lMonster81/p/10433816.html
Leetcode264. Ugly Number II丑数2相关推荐
- LeetCode Ugly Number II(丑数的计算)
代码如下: public class Solution {public int nthUglyNumber(int n) {int[] primes = {2, 3, 5};int[] idx = n ...
- leetcode 264. Ugly Number II
传送门 264. Ugly Number II QuestionEditorial Solution My Submissions Total Accepted: 36259 Total Submis ...
- LeetCode - Medium - 264. Ugly Number II
Topic Math Dynamic Programming Heap Description https://leetcode.com/problems/ugly-number-ii/ Analys ...
- Lintcode 4 Ugly Number II
原题如下: Ugly number is a number that only have factors 2, 3 and 5. Design an algorithm to find the nth ...
- poj 1338 Ugly Numbers(丑数模拟)
转载请注明出处:http://blog.csdn.net/u012860063? viewmode=contents 题目链接:http://poj.org/problem?id=1338 Descr ...
- [leetcode] Ugly Number II
public class Solution { public int nthUglyNumber(int n) { int[] ugly=new int[n+10]; ...
- 动态规划 求第 n 个 Humble Number(丑数)
题目: 质因数分解是将一个数分解为若干个质数相乘的形式,这些因数可以重复.比如 30 = 2×3×5,20 = 2×2× 5,81 = 3×3×3×3.现在我们将质因数分解之后只出现 2,3,5,7 ...
- leetcode 263, 264, 1201, 313. Ugly Number I, II, III, Super Ugly Number(leetcode 丑数问题合集)
263. Ugly Number https://leetcode.com/problems/ugly-number/ 本题题解由下面的 264. Ugly Number II 改造而来,所以效率会比 ...
- LeetCode 264. Ugly Number II--C++,Python解法
题目地址:Ugly Number II - LeetCode Write a program to find the n-th ugly number. Ugly numbers are positi ...
最新文章
- 未来,AI可用于5G网络分析
- linux cpu占用100原因查询,如何根据查询异常时间节点和连接进而确定CPU使用率100%的原因...
- php标签嵌入规范,HTML标签嵌套的详细规则
- 怎么用java实现打字功能_怎么用JAVA编写一个打字游戏
- 论文浅尝 | 利用 KG Embedding 进行问题回答
- 漫画:IT公司年终总结会开崩了...
- DDD(领域驱动设计)系列之一-DomainPrimitive
- spark学习-JavaRDD注册成表然后用SparkSQL查询
- QT Creator 使用 design 修改 ui界面编译后界面未更新代码提示
- Pow(x, n) 求一个数的n次方
- 看我如何挖到 Dropbox Windows 版的这个 0day(微补丁发布)
- Python编程实例(4)
- 【Spark】快速简介
- 项目启动时 xml报错:Could not find SQL statement to include with refid 'mbgl.panDuanZbsfkxg'
- VISIO输出黑白色电路图
- 《从0到1上线微信小游戏》第七节 微信排行榜和好友分享功能
- BP神经网络理解及公式推导
- 我的世界Mods/制作(1)
- “/Ox”和“/RTC1”命令行选项不兼容 或者 ml.exe 退出
- 生物信息-学习从NCBI上下载数据
热门文章
- Django运行报错Invalid HTTP_HOST header: ‘0.0.0.0:8000‘. You may need to add ‘0.0.0.0‘ to ALLOWED_HOSTS.
- 【插件】IDEA中个人觉得最好的插件,附简单使用方法(持续更新)
- poj2253 Frogger dijkstra
- 04-java学习-选择结构
- block引用外部变量原理
- 解读 Q_D, Q_Q 指针
- RadioButton 自定义控件
- android - ADT本地配置、sdk配置
- JavaScript面向对象编程——Array类型
- LAMP源码编译安装配置+wordpress