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)的更多相关文章

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

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

  2. [LeetCode] Ugly Number II

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

  3. LeetCode() Ugly Number II 背下来!

    一个别人,非常牛逼的思路,膜拜了!orz!!!! vector <int> results (1,1); int i = 0, j = 0, k = 0; while (results.s ...

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

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

  5. 【LeetCode】264. Ugly Number II

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

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

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

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

  8. 【刷题-LeetCode】264. Ugly Number II

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

  9. [LeetCode] Ugly Number 丑陋数

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

随机推荐

  1. 在C#中怎么调用Resources文件中的图片

    譬如资源中有名为myPic的图片,在代码中可以这么使用: this.BackgroundImage = Properties.Resources.myPic; 如有疑问,继续追问.

  2. ADO.NET入门教程(三) 连接字符串,你小觑了吗?

    出处:http://www.cnblogs.com/liuhaorain/archive/2012/02/12/2347914.html 摘要 ADO.NET强大的优势在于对不同的数据源提供一致的访问 ...

  3. ORA-12571 : TNS : 包写入程序失败

    错误原因 解决方案 修改D:/oracle/ora92/network/admin目录下sqlnet.ora,将”NAMES.DEFAULT_DOMAIN =” 这一行用#注释掉,将“SQLNET.A ...

  4. C++:对象声明

    (一)类与对象的关系: c++把类的变量叫做类的对象,对象也称类的实例 (二)对象的定义: 1.在声明类的同时,直接定义对象,即在声明类的右花括号“}”后,直接写出 属于该类的对象名表.例如:clas ...

  5. Intellij IDEA新建一个EJB工程(二)

    从博文:http://www.cnblogs.com/yangyquin/p/5328344.html 中可以知道如何利用Intellij IDEA建立一个EJB Module,还有新建一个测试Mod ...

  6. printf left justify

    http://www.lemoda.net/c/printf-left-justify/index.html This example program demonstrates how to left ...

  7. Android真机调试的时候logcat中无法输出调试信息的解决办法

    真机调试不输出日志到logcat的原因是手机厂商默认关闭了调试打印的功能,通过以下方法开启此方法. 下面以华为P6手机为例进行操作: 1.在拨号界面输入:*#*#2846579#*#* 进入测试菜单界 ...

  8. 1934. Black Spot(spfa)

    1934 水题 RE了N久 后来发现是无向图 #include <iostream> #include<cstring> #include<algorithm> # ...

  9. 函数xdes_set_bit

    /**********************************************************************//** Sets a descriptor bit of ...

  10. sql2005主从数据库同步配置

    网站规模到了一定程度之后,该分的也分了,该优化的也做了优化,但是还是不能满足业务上对性能的要求:这时候我们可以考虑使用主从库.主从库是两台服务器上的两个数据库,主库以最快的速度做增删改操作+最新数据的 ...