[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 ...
 
随机推荐
- ios开发图片点击放大
			
图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现 创建了一个专门用于放大图片的类,以下为.h文件 #import <Foundation/Foundation. ...
 - CString, QString, char*之间的转换(包括VC编译开关)
			
传给未分配内存的const char* (LPCTSTR)指针. CString cstr(asdd); const char* ch = (LPCTSTR)cstr; ch指向的地址和cstr相同. ...
 - 为什么国外程序员爱用Mac?
			
Mac 在国外很受欢迎,尤其是在 设计/web开发/IT 人员圈子里.普通用户喜欢 Mac 可以理解,毕竟 Mac 设计美观,简单好用,没有病毒.那么为什么专业人士也对 Mac 情有独钟呢?从个人使用 ...
 - *windows文件显示后缀名
 - python流程控制语句 ifelse - 2
			
#! /usr/bin/python x = input('please input a integer:') x = int (x) ): print('你输入了一个负数') else: print ...
 - GridLayoutManager
			
GridLayoutManager Class Overview A RecyclerView.LayoutManager implementations that lays out items in ...
 - Linux“Bash”漏洞大爆发
			
9月25日,国外曝出一个“毁灭级”的Bash漏洞,黑客可利用此漏洞远程执行任意命令,完全控制目标系统! 该漏洞编号为CVE-2014-6271,主要存在于bash 1.14 - 4.3版本中,受影响的 ...
 - HTMLParser 解析HTML
			
from html.parser import HTMLParser from html.entities import name2codepoint class MyHTMLParser(HTMLP ...
 - Java [Leetcode 328]Odd Even Linked List
			
题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...
 - IPicture、BITMAP、HBITMAP和CBitmap的关系
			
1.有关IPicture加载图片后直接Render到内存DC的问题(HBITMAP转换IPicture)Picture的方法get_Handle可以直接得到图片的句柄 IPicture *pIPict ...