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];
}
};
Leetcode264. Ugly Number II丑数2的更多相关文章
- 264 Ugly Number II 丑数 II
编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...
- [LeetCode] 264. Ugly Number II 丑陋数 II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode OJ:Ugly Number(丑数)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] 264. Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了
丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...
- 【easy】263. Ugly Number 判断丑数
class Solution { public: bool isUgly(int num) { ) return false; ) return true; && num % == ) ...
- LeetCode OJ 之 Ugly Number (丑数)
题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...
- 313 Super Ugly Number 超级丑数
编写一段程序来寻找第 n 个超级丑数.超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数.例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] ...
随机推荐
- 第四周课堂笔记3th
1.函数的嵌套 作用域,说的是变量 全局作用域:内置命名空间,全局命名空间 全局空间不可以引用局部空间 局部作用域: 局部命名空间 开辟的临时空间前提是调用了函数 全局作用域在整个文件中被使用 L ...
- where与having区别
解释一. 聚合函数是比较where.having 的关键. 开门见山.where.聚合函数.having 在from后面的执行顺序: where>聚合函数(sum,min,max,avg,cou ...
- vue 利用intersectionOberver实现全局appear/disappear事件
搬运自:https://juejin.im/post/5cd10959f265da03a00fe5c6 效果: demo地址: https://codepen.io/deepkolos/pen/OYP ...
- idea-----Intellij IDEA配置tomcat(非maven项目)
Intellij IDEA配置tomcat(非maven项目) 引用: https://blog.csdn.net/springlovejava/article/details/78570241 ID ...
- synchronized ReentrantLock 比较分析
在编写多线程代码的时候,对于不允许并发的代码,很多需要加锁进行处理.在进行加锁处理时候,synchronized作为java的内置锁,同时也是java关键字,最为被人熟知,即使是最初级的java程序员 ...
- React中的this.props.children
React this.props.children this.props对象的属性与组件的属性一一对应,但是有一个例外,就是this.props.children属性.它表示组件的所有子节点. var ...
- We'll be fine.
可怜的人心中都有一种信念,那就是 明天会更好. Everything will be fine. 我拥见风来,嗅见花开,是避不掉的厉寒,是止不住的徘徊.
- php输出json,需要嵌套数组和对象问题
https://segmentfault.com/q/1010000009985295 $tmp = []; $tmp['id'] = 'aaa'; $tmp['name'] = 'bbb'; $tm ...
- 05_jQuery对象初识(三)登录案例
1.案例需求:点击登录按钮验证用户名和密码都不为空,为空就在对应的input标签下面显示一个错误的提示信息. 1.给登录的按钮绑定点击事件 2.点击事件要做的事情 1.找到input标签.取值.判断是 ...
- SDOI2019 R2退役记
还是退役了呀 Day -1 早上loli发了套题结果啥都不会 之后胡爷爷就秒了道数据结构 不过也没什么人做,于是全机房都在愉快的划水 下午来机房打了场luogu的\(rated\)赛,还是啥都不会 之 ...