[LeetCode] Ugly Number II (A New Question Added Today)
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.
这也是今天新加的一道题。挺有意思的。~
这道题如果找不到方法的话会很难,但是也不是太难,只能说算法会很复杂。这里还是需要借助一些数学知识。
这道题的思路借鉴于:http://www.geeksforgeeks.org/ugly-numbers/
经过观察ugly number sequence,我们发现其实可以将这个sequence分解为三个小的sequence。(因为一共三个prime factor嘛)
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, …
(1) 1×2, 2×2, 3×2, 4×2, 5×2, …
(2) 1×3, 2×3, 3×3, 4×3, 5×3, …
(3) 1×5, 2×5, 3×5, 4×5, 5×5, …
我觉得经过观察上面这三个sequence,应该大家可以找到规律了。
每一个分解出来的sequence就等于原来的ugly number sequence中的每个数乘以2/3/5。
因此我们可以用merge sort依次从这三个分解出的sequence里面依次挑出ugly number。借助Math.min()method。
代码如下。~
public class Solution {
public int nthUglyNumber(int n) {
int[] result=new int[n];
result[0]=1;
int a=0;
int b=0;
int c=0;
int factora=2;
int factorb=3;
int factorc=5;
for(int i=1;i<n;i++){
int min=Math.min(Math.min(factora,factorb),factorc);
result[i]=min;
if(factora==min){
factora=2*result[++a];
}
if(factorb==min){
factorb=3*result[++b];
}
if(factorc==min){
factorc=5*result[++c];
}
}
return result[n-1];
}
}
[LeetCode] Ugly Number II (A New Question Added Today)的更多相关文章
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode() Ugly Number II 背下来!
一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.s ...
- Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)
Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 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. Ugly Number II (medium)
263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...
- 【刷题-LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- [LeetCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
随机推荐
- 10 harsh truths that will help you grow
10 harsh truths that will help you grow帮你成长的10个残酷事实In the game of life, if it often seems like you’r ...
- mysql外键级联更新删除
MySQL支持外键的存储引擎只有InnoDB,在创建外键的时候,要求父表必须有对应的索引,子表在创建外键的时候也会自动创建对应的索引.在创建索引的时候,可以指定在删除.更新父表时,对子表进行的相应操作 ...
- linux自动启动shell和init概述(fedora use systemmd now!!!)
linux运行级别 linux启动之后会在一个级别运行,下面列出了这些运行级别: 0 系统停止 1 单用户系统,不需要登陆 2 多用户系统但不支持NFS,命令行模式登陆 3 完整多用户模式,命令行模 ...
- ubuntu 乱码 改为英文
http://878045653.blog.51cto.com/2693110/735654 解决方法: 改成全英文环境来解决 方格 乱码 : 用vim配置语言环境变量 vim / etc/envir ...
- 三星原厂就K9K8G08U0D升级为K9K8G08U0E的回信
1. please check the below timing first. K9F1G08U0E vs K9F1G08U0D Timing difference tR tPROG tBERS N ...
- [原]用WebBrowser组件模拟人工运行搜索引擎自动点击搜索结果的实验
本代码只是业余时间无聊写着试试,用WebBrowser组件模拟人工运行搜索引擎自动点击搜索结果的实验 这是网络中盛传的提高搜索引擎点击率的一种方式,当然属于作弊,不推荐各位使用.另外这种方式的性能不佳 ...
- 宏HASH_GET_FIRST
/*******************************************************************//** Gets the first struct in a ...
- bzoj2800
这题好难,翻了一下波兰文的题解……这好像是当年唯一没人A的题目 首先区间修改不难想到差分,我们令d1=x1,dn+1=-xn,di=xi-xi-1 注意Σdi=0,这样对于[l,r]的修改(比如+a) ...
- CodeForces 489C (贪心) Given Length and Sum of Digits...
题意: 找出m位且各个数位数字之和为s的最大和最小整数,不包括前导0(比如说003是非法的),但0是可以的. 分析: 这题是用贪心来做的,同样是m位数,前面的数字越大这个数就越大. 所以写一个can( ...
- 图表框架HelloCharts(1)线形图
效果图 1. 导入 .aar 2. fragment_line_chart.xml <RelativeLayout xmlns:android="http://schemas.andr ...