264 Ugly Number II 丑数 II
编写程序找第 n 个丑数。
丑数就是只包含质因子 2, 3, 5 的正整数。
例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数。
注意:
1. 1 一般也被当做丑数
2. n不超过1690
详见:https://leetcode.com/problems/ugly-number-ii/description/
Java实现:
class Solution {
public int nthUglyNumber(int n) {
if(n<=1){
return n;
}
int[] res=new int[n];
res[0]=1;
int i2=0,i3=0,i5=0;
for(int i=1;i<n;++i){
res[i]=Math.min(res[i2]*2,Math.min(res[i3]*3,res[i5]*5));
if(res[i]==res[i2]*2){
++i2;
}
if(res[i]==res[i3]*3){
++i3;
}
if(res[i]==res[i5]*5){
++i5;
}
}
return res[n-1];
}
}
C++实现:
class Solution {
public:
int nthUglyNumber(int n) {
if(n<=1)
{
return n;
}
vector<int> res(n);
res[0]=1;
int i2=0,i3=0,i5=0;
for(int i=1;i<n;++i)
{
res[i]=min(min(res[i2]*2,res[i3]*3),res[i5]*5);
if(res[i]==res[i2]*2)
{
++i2;
}
if(res[i]==res[i3]*3)
{
++i3;
}
if(res[i]==res[i5]*5)
{
++i5;
}
}
return res[n-1];
}
};
264 Ugly Number II 丑数 II的更多相关文章
- LeetCode OJ:Ugly Number(丑数)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- [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] ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- [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 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- leetcode 264. 丑数 II 及 313. 超级丑数
264. 丑数 II 题目描述 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, ...
随机推荐
- Ubuntu 16.04添加多张虚拟网卡
1.添加 sudo ifconfig enp0s31f6:0 192.168.10.10 up 2.卸载 sudo ifconfig enp0s31f6:0 down 注意:enp0s31f6每台电脑 ...
- 【Windows系统】-- 远程桌面时,WIN键被锁定
问题重现: 在对远程机器进行操作的时候,按键时会自动变成WIN组合键,比如:你按D的效果为[WIN+D]组合键的效果 就是切换到桌面,按E就是[WIN+E]组合键的效果,就是打开资源管理器. 解决方案 ...
- ArcGIS for Android入门程序之DrawTool2.0
来自:http://blog.csdn.net/arcgis_mobile/article/details/8084763 GISpace博客<ArcGIS for Android入门程序之Dr ...
- Python学习系列之异常处理
什么是异常处理 python内置了一套try···except···finally的错误处理机制 当程序出错的时候进行捕捉,然后根据捕捉到的错误信息进行响相应的处理 常用的内建异常 初识异常处理 如例 ...
- UVa 10950 - Bad Code
题目:有一种编码方式.串仅仅有小写字母构成,每一个小写字母相应一个数字,如今给你妆化后的数字串, 问有多少个原串与之相应,注意数字串里可能有一个前导0. 分析:搜索.按字母顺序存储映射表,按字母顺序匹 ...
- 浅谈MySQL load data local infile细节 -- 从源码层面
相信大伙对mysql的load data local infile并不陌生,今天来巩固一下这里面隐藏的一些细节,对于想自己动手开发一个mysql客户端有哪些点需要注意的呢? 首先,了解一下流程: 3个 ...
- uva 10765 Doves and Bombs(割顶)
题意:给定一个n个点的连通的无向图,一个点的"鸽子值"定义为将它从图中删去后连通块的个数.求每一个点的"鸽子值". 思路dfs检查每一个点是否为割顶,并标 ...
- Codeforces Round #313 B. Gerald is into Art(简单题)
B. Gerald is into Art time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- jeasyui控件事件和方法的使用方法
1.事件是在页面加载后就注册绑定: $(function () { ocr(); }); function ocr() { $('#dgTeacher').datagrid({ onClickRow: ...
- oracle数据库的导入 导出实例
oracle数据库的导入 导出实例 分类: DataBase2011-09-07 23:25 377人阅读 评论(0) 收藏 举报 数据库oraclefileusercmdservice 我要从另外一 ...