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. Linux 2.4.x内核软中断机制

    http://www.ibm.com/developerworks/cn/linux/kernel/interrupt/ 软中断概况 软中断是利用硬件中断的概念,用软件方式进行模拟,实现宏观上的异步执 ...

  2. C和指针贴图

    ANSI C 算术转换 内存操作函数 打开流 关闭流 IO函数常用模式 字符输入函数 字符输出函数 撤销字符 未格式化的行IO 格式化的行IO-scanf家族 格式化IO-printf家族 print ...

  3. 249. Group Shifted Strings

    题目: Given a string, we can "shift" each of its letter to its successive letter, for exampl ...

  4. mac root用户初始密码设置

    具体方法如下: 1)sudo su切换到root,输入的用户密码是当前用户的密码: 2)切换到root后,执行passwd root,设置root用户密码即可.

  5. Android handler.obtainMessage()

    在handler.obtainMessage()的参数是这样写的: Message android.os.Handler.obtainMessage(int what, int arg1, int a ...

  6. OpenCV源码阅读(3)---matx.h---学习心得

    在.h文件里定义类,可以通过内联函数的方法完成类基础函数的实现,这样就不需要额外写.cpp文件来写类的内容. 对于操作符重载,可以使用返回应用的方式减小内存开销 _Tp& someclass: ...

  7. MySQL 5.7 SYS scheme解析

    sys 库是MySQL 5.7其中的一个系统库,里面有很多很好用的跟性能相关的视图.函数和存储过程, 增强MySQL的易用性 例如:哪些语句使用了临时表,哪个用户请求了最多的io,哪个线程占用了最多的 ...

  8. Android的底层库libutils介绍

    第一部分 libutils概述 libutils是Android的底层库,这个库以C++实现,它提供的API也是C++的.Android的层次的C语言程序和库,大都基于libutils开发. libu ...

  9. AutoResetEvent

    private static readonly AutoResetEvent autoResetEvent = new AutoResetEvent(false); private static vo ...

  10. fil_system_struct

    /** The tablespace memory cache */ typedef struct fil_system_struct fil_system_t; /** The tablespace ...