[leetcode] 264. Ugly Number II 解题报告
2015-12-13 15:54
411 查看
题目链接:https://leetcode.com/problems/ugly-number-ii/
Write a program to find the
Ugly numbers are positive numbers whose prime factors only include
Note that
Hint:
The naive approach is to call
every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1,
L2, and L3.
Assume you have Uk, the kth ugly
number. Then Uk+1 must be Min(L1 *
2, L2 * 3, L3 * 5).
思路:由题目提示可以知道,我们可以依次判断每一个数直到找到第n个丑数,但是因为大多数并不是丑数,由丑数的规律--质因数只有2,3,5,我们可以依次只产生丑数,这样效率会有很大的提高。但是我们要如何保持有序的产生丑数呢?
假设我们现在已经有了一个丑数的有序数组,如果要找到下一个丑数,则可以将数组中的每一个数乘以2,并将其中第一个大于当前丑数的的结果记为M2,同样将当前有序数组每一个数都乘以3,第一个大于当前丑数的的结果记为M3,同样方式得到乘以5的第一个大于当前丑数的结果记为M5。可以下一个丑数必然是min(M2,
M3, M5)。
事实上我们并不必要将数组中的每个数都乘以2,3,5。对于乘以2来说,我们只要找到第一个乘以2大于当前丑数的数在数组中的位置,同样找到第一个乘以3,5大于当前丑数的数的位置。如果当前丑数记为M,然后就可以使用min(M*2, M*3, M*5)来产生下一个丑数。
代码如下:
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, 12is the sequence of the first
10ugly numbers.
Note that
1is typically treated as an ugly number.
Hint:
The naive approach is to call
isUglyfor
every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones.
An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1,
L2, and L3.
Assume you have Uk, the kth ugly
number. Then Uk+1 must be Min(L1 *
2, L2 * 3, L3 * 5).
思路:由题目提示可以知道,我们可以依次判断每一个数直到找到第n个丑数,但是因为大多数并不是丑数,由丑数的规律--质因数只有2,3,5,我们可以依次只产生丑数,这样效率会有很大的提高。但是我们要如何保持有序的产生丑数呢?
假设我们现在已经有了一个丑数的有序数组,如果要找到下一个丑数,则可以将数组中的每一个数乘以2,并将其中第一个大于当前丑数的的结果记为M2,同样将当前有序数组每一个数都乘以3,第一个大于当前丑数的的结果记为M3,同样方式得到乘以5的第一个大于当前丑数的结果记为M5。可以下一个丑数必然是min(M2,
M3, M5)。
事实上我们并不必要将数组中的每个数都乘以2,3,5。对于乘以2来说,我们只要找到第一个乘以2大于当前丑数的数在数组中的位置,同样找到第一个乘以3,5大于当前丑数的数的位置。如果当前丑数记为M,然后就可以使用min(M*2, M*3, M*5)来产生下一个丑数。
代码如下:
class Solution { public: int nthUglyNumber(int n) { vector<int> vec(1,1); int k2 = 0, k3 = 0, k5 = 0, cur = 0; while(++cur < n) { int val = min(vec[k2]*2, min(vec[k3]*3, vec[k5]*5)); vec.push_back(val); while(vec[k2]*2 <= vec[cur]) k2++; while(vec[k3]*3 <= vec[cur]) k3++; while(vec[k5]*5 <= vec[cur]) k5++; } return vec[n-1]; } };
相关文章推荐
- 推荐Sql Server的博客
- 将数据保存到sd卡中
- Tsinsen-A1490 osu! 【数学期望】
- Linux下安装JDK和tomcat
- IOS中的Block的使用
- 【数组】将给定字符串str="1,2,3!4,5,6,0!7,8,9"存入二维数组中。(使用 split(String regex) ) int[][] a
- Java中ArrayList 元素的排序
- 一个小菜鸟对数据在内存中的二进制存储的理解
- iOS UI 04 轨道和动画
- 大爱jQuery,10美女模特有用jQuery/CSS3插入(集成点免费下载)
- iOS UI 04 轨道和动画
- js 练习
- Document,Node,Element,HTMLDocument ,HTMLCollection,HTMLElement,NodeList
- [Coursera]算法基础_Week3_递归_Q1
- 面试题 寻找缺失的页码
- setContentView (Java)
- poj 3565 Ants KM算法
- iOS UI 03 协议代理
- #学习笔记#(15)photoshop动感雷达
- iOS UI 03 协议代理