编写程序找第 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的更多相关文章

  1. LeetCode OJ:Ugly Number(丑数)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  2. [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了

    丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...

  3. 【easy】263. Ugly Number 判断丑数

    class Solution { public: bool isUgly(int num) { ) return false; ) return true; && num % == ) ...

  4. LeetCode OJ 之 Ugly Number (丑数)

    题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...

  5. 313 Super Ugly Number 超级丑数

    编写一段程序来寻找第 n 个超级丑数.超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数.例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] ...

  6. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

  7. [LeetCode] 264. Ugly Number II 丑陋数 II

    Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...

  8. 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 ...

  9. leetcode 264. 丑数 II 及 313. 超级丑数

    264. 丑数 II 题目描述 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, ...

随机推荐

  1. Strongly connected-HDU4635

    Problem - 4635 http://acm.hdu.edu.cn/showproblem.php?pid=4635 题目大意: n个点,m条边,求最多再加几条边,然后这个图不是强连通 分析: ...

  2. Codeforces 799E(贪心)

    题意: 有n个物品,购买物品i需要花费ci的代价.Arkady和Masha分别有喜欢的物品. 现在需要从中选m个,使得这m个物品中至少有k个Arkady喜欢的物品,k个Masha喜欢的物品. 输出满足 ...

  3. JS中 为什么很多要用两个!! 来判断

    比如 if(!!last) 这个就表示 if(last || false).将判断的类型,强转成boolean类型.如果last是null(或者undefine)的话,!last,返回的就是true ...

  4. Memcached与Spring集成的方式(待实践)

    主要是基于这几种方式http://www.cnblogs.com/EasonJim/p/7624822.html去实现与Spring集成,而个人建议使用Xmemcached去集成好一些,因为现在官方还 ...

  5. IOS开发UI篇--一个支持图文混排的ActionSheet

    一.简单介绍 UIActionSheet是IOS提供给我们开发人员的底部弹出菜单控件.一般用于菜单选择.操作确认.删除确认等功能.IOS官方提供的下面方式对UIActionView进行实例化: - ( ...

  6. Android应用程序安装过程浅析

    我们知道在android中.安装应用是由PackageManager来管理的,可是我们发现PackageManager是一个抽象类.他的installPackage方法也没有详细的实现. 那在安装过程 ...

  7. react 项目实战(六)提取布局组件

    重复代码是混乱的根源!,本篇文章我们来继续消灭重复代码. 目标 细心的同学应该能发现:每一个Page组件(/src/pages下的组件)的render方法都拥有相似的jsx结构,比如: render ...

  8. mybatis中批量插入数据

    <insert id="insertIntoDevAct" parameterType="java.util.List"><foreach c ...

  9. 【v2.x OGE-example 第三节 播放精灵动画】

    1. 位置:Drawing_example --> SpriteAnimated 2. 类名:SpriteAnimated 3.利用AnimatedSprite动画精灵类能够实现多种多种动作. ...

  10. Linux 内核开发 - 内存管理

    1.1什么是内存管理 内存管理是对计算机内存进行分配和使用的技术.内存管理主要存在于多任务的操作系统中,因为内存资源极其有限.须要在不同的任务之间共享内存,内存管理的存在就是要高效.高速的非配内存,并 ...