编写程序找第 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. Delphi DBGrid实现多选

    DBGrid1.Options:= DBGrid1.Options+[dgMultiSelect];  //先设置DBGrid1的多选属性为True if DBGrid1.SelectedRows.C ...

  2. 深入理解hadoop(三)

    Hadoop多用户作业调度器 hadoop 最初是为批处理作业设计的,当时只采用了一个简单的FIFO调度机制分配任务,随着hadoop的普及以及应用的用户越来越多,基于FIFO的单用户调度机制不能很好 ...

  3. Mac 系统引导过程概述 & BootCamp 的秘密

    http://bbs.feng.com/read-htm-tid-6890655.html

  4. 基于TensorFlow的图片识别服务

    1.使用TensorFlow Retrain进行图片分类训练 https://www.tensorflow.org/versions/master/how_tos/image_retraining/i ...

  5. RIP

    距离矢量路由协议 假设网络拓扑如下 192.168.1.0网段 - - - - R1 - - 192.168.12.0网段 - - R2 - - 192.168.23.0网段 - - R3 - - - ...

  6. 【大数据处理架构】1.spark streaming

    1. spark 是什么? >Apache Spark 是一个类似hadoop的开源高速集群运算环境  与后者不同的是,spark更快(官方的说法是快近100倍).提供高层JAVA,Scala, ...

  7. Mariadb 索引及外键

    索引 索引相当于一本书的目录,在一个数据库或表有索引的情况下,会很便于查询数据,使查询更加效率,相对的也有缺点,不利于去修改,比较麻烦,有索引便于查询,那就意味着索引创建的越多越好么?然而并不是:索引 ...

  8. ORACLE取周、月、季、年的開始时间和结束时间

     1           取周的開始时间和结束时间 取周的開始时间.以星期一为開始. SQL>SELECT TRUNC(TO_DATE('2013-11-25 10:31:11','YYYY ...

  9. Android在onCreate()方法中动态获取TextView控件的高度

    正好朋友项目里遇到了给写了个小Demo: 这个监听器看名字也知道了.就是在绘画完毕之前调用的,在这里面能够获取到行数.当然也能够获取到宽高等信息 package com.example.textvie ...

  10. 基于百度AI人脸识别技术的Demo

    编写demo之前首先浏览官方API:http://ai.baidu.com/docs#/Face-API/top 下面是源码: package com.examsafety.test; import ...