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. ubuntu 截图工具 Shutter,设置快捷键 Ctrl+Alt+A

    系统设置 键盘 快捷键 自定义快捷键

  2. [noip模拟赛]算算数

    https://www.zybuluo.com/ysner/note/1298755 题面 有一天小胡同学看到了一种表达式.这个表达式有四个变量\(A,B,C,D\).这四 个变量都只有\(0\)和\ ...

  3. .NET 导入导出Excel

    第一种方式:OleDb 需要安装office,且读数据慢,而且有数据格式的Cell读出数据不正确等问题.放弃. 第二种方式:NPOI开源库 使用NPOI导入导出Excel应该是.NET开发很常用的手段 ...

  4. 用React & Webpack构建前端新闻网页

    这是一篇给初学者的教程, 在这篇教程中我们将通过构建一个 Hacker News 的前端页面来学习 React 与 Webpack. 它不会覆盖所有的技术细节, 因此它不会使一个初学者变成大师, 但希 ...

  5. WP8 中使用HTML Agility Pack与友盟分享SDK遇到的 System.Xml.XPath加载问题

    今晚在尝试使用友盟最新的社交分享SDK时,按照官方Demo,并未做多少多少改动,就是去除了对微信.脸书和推特的分享.然后运行之后就一直报错 : {System.IO.FileLoadException ...

  6. 第2章 微信小程序的基础组件学习

    小程序也可以用div+css进行排版. flex-direction排列方向,可以控制内部的成员的顺序,比如从左到右.从右到左.上下,纵向和横向. flex-wrap可以控制换行是如何去换行的,控制它 ...

  7. CentOS 7 配置 Nginx 正向代理 http、https 最详解

    手头项目中有使用到 nginx,因为使用的三方云服务器,想上外网需要购买外网IP的,可是有些需要用到外网却不常用的主机也挂个外网IP有点浪费了,便想使用nginx的反向代理来实现多台内网服务器使用一台 ...

  8. 【js】callback时代的变更

    最近团队开始越来越多的使用es7标准的async/await,从最开始的promise到后面的generator,再到现在async,对于异步,每个时期都有着其特有的解决方案,今天笔者就以自己的接触为 ...

  9. Python中re操作正则表达式

    在python中使用正则表达式 1.转义符 正则表达式中的转义: '\('表示匹配小括号 [()+*/?&.] 在字符组中一些特殊的字符会现出原形 所有的\s\d\w\S\D\W\n\t都表示 ...

  10. 暴力+构造 Codeforces Round #283 (Div. 2) C. Removing Columns

    题目传送门 /* 题意:删除若干行,使得n行字符串成递增排序 暴力+构造:从前往后枚举列,当之前的顺序已经正确时,之后就不用考虑了,这样删列最小 */ /*********************** ...