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。

代码如下。~

  1. public class Solution {
  2. public int nthUglyNumber(int n) {
  3. int[] result=new int[n];
  4. result[0]=1;
  5. int a=0;
  6. int b=0;
  7. int c=0;
  8. int factora=2;
  9. int factorb=3;
  10. int factorc=5;
  11. for(int i=1;i<n;i++){
  12. int min=Math.min(Math.min(factora,factorb),factorc);
  13. result[i]=min;
  14. if(factora==min){
  15. factora=2*result[++a];
  16. }
  17. if(factorb==min){
  18. factorb=3*result[++b];
  19. }
  20. if(factorc==min){
  21. factorc=5*result[++c];
  22. }
  23. }
  24. return result[n-1];
  25. }
  26. }

[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. PHP Warning: date(): It is not safe to rely on the system's timezone settings.

    OSSEC安装结束后运行,运行以下命令却抛错 cat /opt/ossec/logs/alerts/alerts.log 具体抛错内容: ** Alert 1468897672.2164786: ma ...

  2. linux 开机自启动软件(包含xampp方法)

    linux设置apache和mysql: linux开启启动的程序一般放在/etc/rc.d/init.d/里面,/etc/init.d/是其软连接. mysql设为linux服务 cp /usr/l ...

  3. 三个特殊资源目录 /res/xml /res/raw 和 /assets

    在android开发中,我们离不开资源文件的使用,从drawable到string,再到layout,这些资源都为我们的开发提供了极大的便利,不过我们平时大部分时间接触的资源目录一般都是下面这三个. ...

  4. iOS方法封装

    (void) setSubView:(UIView *)masterView subCCGRect:(CGRect)subCCGRect imageName:(NSString *)imageName ...

  5. Androidz之Activity概要学习

    Androidz之Activity概要学习 1.     Activity类概述 Activity(活动)是一个单独的.能获取焦点的,且能与用户交互的东西.所以我们通常在Activity类中的onCr ...

  6. Android手机拍照

    参考的这个视频教程:http://v.youku.com/v_show/id_XNjI5MzkzMjQ4.html和官方API档file:///D:/Android/androidstudio/sdk ...

  7. km算法的个人理解

    首先相对于上个blog讲的匈牙利算法用于解决无权二分图的最佳匹配,km算法则是在匈牙利算法基础上更进一层的,每条边增加了权值后,真的开始看时有些无厘头,觉得没有什么好方法,但两位牛人Kuhn-Munk ...

  8. fil_space_create

    /*******************************************************************//** Creates a space memory obje ...

  9. ASP.NET 共用类库1

    using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...

  10. Tomcat 7.0配置SSL的问题及解决办法

    http://dong-shuai22-126-com.iteye.com/blog/1830209   以前一直在用Tomcat 6.0.29版本,今下载了apache-tomcat-7.0.33- ...