一. 题目描写叙述

Write a program to find the n-th ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers.

Note that 1 is typically treated as an ugly number.

二. 题目分析

关于丑数的概念,可參考Ugly Number

从1開始的丑数为:1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … 该题的大意是,输入一个正整数n,返回第n个丑数。这要求比起Ugly Number一题是复杂了些。

其实,在观察这些丑数组合时,无非是分成例如以下三种组合(当中。第一个乘数为上一次计算得出的丑数,第一个丑数为1,第二个乘数为2、3、5中的一个数):

(以2为乘数)1×2, 2×2, 3×2, 4×2, 5×2, 6×2, 8×2, …
(以3为乘数)1×3, 2×3, 3×3, 4×3, 5×3, 6×3, 8×3, …
(以5为乘数)1×5, 2×5, 3×5, 4×5, 5×5, 6×5, 8×5, …

于是。开辟一个存放n个丑数的数组。在每次迭代时,从三种乘法组合中选取积最小的丑数并放入数组。最后数组的最后一个元素即是所求的丑数。

三. 演示样例代码

class Solution
{
public:
int nthUglyNumber(int n) {
int* uglyNum = new int[n]; // 用于存放前n个丑数
uglyNum[0] = 1; int factor2 = 2, factor3 = 3, factor5 = 5;
int index2, index3, index5;
index2 = index3 = index5 = 0; for(int i = 1; i < n; ++i)
{
// 取三组中的最小
int minNum = min(factor2, factor3, factor5);
uglyNum[i] = minNum; // 分三组计算
if(factor2 == minNum)
factor2 = 2 * uglyNum[++index2];
if(factor3 == minNum)
factor3 = 3 * uglyNum[++index3];
if(factor5 == minNum)
factor5 = 5 * uglyNum[++index5];
}
int temp = uglyNum[n-1];
delete [] uglyNum;
return temp;
} private:
// 求三个数的最小值
int min(int a, int b, int c) {
int minNum = a > b ? b : a;
return minNum > c ? c : minNum;
}
};

leetcode笔记:Ugly Number II的更多相关文章

  1. [leetcode] 264. Ugly Number II (medium)

    263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...

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

  3. [LeetCode] 264. Ugly Number II 丑陋数之二

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

  4. Leetcode 264. Ugly Number II

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

  5. (medium)LeetCode 264.Ugly Number II

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

  6. LeetCode——264. Ugly Number II

    题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...

  7. leetcode@ [263/264] Ugly Numbers & Ugly Number II

    https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...

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

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

  9. 【LeetCode】264. Ugly Number II

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

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

随机推荐

  1. 【两种方式 Service References和 web References 】手把手教你引入webservice 服务

    1.对于一个webservie服务我们如何引入到自己的项目中去呢 第一种方法[Service References]:鼠标移到属性上 右键添加服务引用 然后在地址栏输入webservice 地址 点击 ...

  2. poj 3278 catch that cow BFS(基础水)

    Catch That Cow Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 61826   Accepted: 19329 ...

  3. maven国内镜像

    <?xml version="1.0" encoding="UTF-8"?> <!--Licensed to the Apache Softw ...

  4. BZOJ3997 [TJOI2015]组合数学 【Dilworth定理】

    题目 给出一个网格图,其中某些格子有财宝,每次从左上角出发,只能向下或右走.问至少走多少次才能将财宝捡完.此对此问题变形,假设每个格子中有好多财宝,而每一次经过一个格子至多只能捡走一块财宝,至少走多少 ...

  5. 流浪者(rover)

    流浪者(rover) 题目描述 有一位流浪者正在一个n∗mn∗m的网格图上流浪.初始时流浪者拥有SS点体力值. 流浪者会从(1,1)(1,1)走向(n,m)(n,m),并且他只会向下走((x,y)→( ...

  6. 为MYSQL加注释--mysql注释符

    上午插入记录的时候一直没有成功,郁闷不知道为什么.因为是很多条记录一起插入,中间一些不用的数据就用"--"来注释了,结果没有效果. 没有办法,在网上找了找,才发现注释符" ...

  7. Master of Sequence

    Master of Sequence 时间限制: 10 Sec  内存限制: 128 MB 题目描述 There are two sequences a1,a2,...,an , b1,b2,..., ...

  8. event.srcElement就是指向触发事件的元素,他是什么就有什么的属性

    原文发布时间为:2009-06-29 -- 来源于本人的百度文章 [由搬家工具导入] 得到或设置触发事件的对象。   event.srcElement就是指向触发事件的元素,他是什么就有什么的属性 s ...

  9. 完美CSS文档的8个最佳实践

      在css的世界,文档没有被得到充分的利用.由于文档对终端用户不可见,因此它的价值常常被忽视.另外,如果你第一次为css编写文档,可能很难确定哪些内容值得记录,以及如何能够高效完成编写. 然而,为C ...

  10. [LeetCode] Add Two Numbers 链表

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...