Description

Ugly number is a number that only havefactors 23 and 5.

Design an algorithm to find the nth ugly number. The first 10 ugly numbers are 1, 2, 3, 4, 5, 6, 8, 9, 10, 12...

Example

If n=9, return 10.

Challenge

O(n log n) or O(n) time.

Notice

Note that 1 is typically treated as an ugly number.

Answer

     /**
* @param n: An integer
* @return: the nth prime number as description.
*/
public int nthUglyNumber(int n) {
// find out the nth ugly number.
int checker,count=;
int num=; do{
checker = num; while( checker% == ) checker /= ;
while( checker% == ) checker /= ;
while( checker% == ) checker /= ;
if( checker== )
{
count++;
}
if( count == n )
{
return num;
} num++;
}while(true);
}

Better Solution

     /**
* @param n: An integer
* @return: the nth prime number as description.
*/
int nthUglyNumber(int n) {
// find out the nth ugly number.
int* uglyNum = new int[n];
int factor2=, factor3=, factor5=;
int index2=, index3=, index5=; uglyNum[]=; for(int i=; i<n; i++)
{
factor2 = uglyNum[index2]*; factor3 = uglyNum[index3]*; factor5 = uglyNum[index5]*;
int minNum = min(factor2, min(factor3, factor5) );
uglyNum[i] = minNum;
if ( minNum == factor2 ) index2++;
if ( minNum == factor3 ) index3++;
if ( minNum == factor5 ) index5++;
} return uglyNum[n-];
}

Hints

  1. The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are notugly. Try to focus your effort on generating only the ugly ones.
  2. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
  3. The key is how to maintain the order of the ugly numbers. Try a similar approach of merging from three sorted lists: L1, L2, and L3.
  4. Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).
  (1) 1x2,  2x2, 2x2, 3x2, 3x24x2, 5x2...
  (2) 1x3,  1x3, 2x3, 2x3, 2x3, 3x3, 3x3...
  (3) 1x5,  1x5, 1x5, 1x5, 2x5, 2x5, 2x5...
 

[Algorithm] 4. Ugly Number II的更多相关文章

  1. 【LeetCode】264. Ugly Number II

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

  2. Ugly Number,Ugly Number II,Super Ugly Number

    一.Ugly Number Write a program to check whether a given number is an ugly number. Ugly numbers are po ...

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

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

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

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

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

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

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

  7. leetcode@ [263/264] Ugly Numbers & Ugly Number II

    https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...

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

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

  9. Leetcode 264. Ugly Number II

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

随机推荐

  1. UVA 213 Message Decoding 【模拟】

    题目链接: https://cn.vjudge.net/problem/UVA-213 https://uva.onlinejudge.org/index.php?option=com_onlinej ...

  2. 电脑的系统盘只有10G了

    软件也是缓存多了,准备把一些让家人卸载,昨天开始布置培训任务就是有一个电报的程序,把流程说了下,从今天开始就会指导,错误点分析.

  3. POJ1279 Art Gallery 多边形的核

    POJ1279 给一个多边形 求它的核的面积 所谓多边形的核 是多边形中的一个点集 满足其中的点与多边形边上的点的连线全部在多边形中 用多边形的每一条边所在的直线去切整个坐标平面 得到的一个凸包就是核 ...

  4. 响应在此上下文中不可用 asp.net

    (一)实例1: 在asp.net程序中添加了一个 类.cs 如下 using System; using System.Collections; using System.ComponentModel ...

  5. 关于file文件操作的头文件 【LINUX】 (转载)

    转自:http://blog.csdn.net/figo77ll/article/details/3156052 Linux下如果要对文件进行读取访问,需要包含至少以下两个头文件: #inlcude ...

  6. 慕课网JavaScript函数1-20 作业:函数的基础封装

    1-20 作业 小伙伴们,掌握了JavaScript的语法.流程控制语句以及函数,接下来让我们运用所学知识完成如gif图所示的效果——计算自己出生那天是该年当中的第几天. gif效果图如下: 任务描述 ...

  7. [SQL必知必会] 读书笔记

    第1课 数据库 这一课介绍SQL究竟是什么,它能做什么事情.   1.1 数据库基础 下面是一些数据库概念的简要介绍,如果你刚开始接触数据库,可以由此了解必需的基本知识.   1.1.1 数据库 数据 ...

  8. FFT学习及简单应用(一点点详细)

    什么是FFT 既然打开了这篇博客,大家肯定都已经对FFT(Fast Fourier Transformation)有一点点了解了吧 FFT即为快速傅里叶变换,可以快速求卷积(当然不止这一些应用,但是我 ...

  9. 思维题+set URAL 1718 Rejudge

    题目传送门 /* 题意:数据加10组,再删掉第6组数据,问rejudge后最少最多几个作者收到邮件 思维题:当错在6时结果是不一定,错在7时是一定改变,因为会变成6 思路没错,但用结构题排序一直WA, ...

  10. Too many open files故障解决一例

    Linux环境WebSphere输出日志: [// ::: EDT] 000090b7 SystemErr R Caused by: java.io.FileNotFoundException: /o ...