[Algorithm] 4. Ugly Number II
Description
Ugly number is a number that only havefactors 2
, 3
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
- 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. - An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number.
- 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.
- Assume you have Uk, the kth ugly number. Then Uk+1 must be Min(L1 * 2, L2 * 3, L3 * 5).
[Algorithm] 4. Ugly Number II的更多相关文章
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 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 ...
- 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之动态规划(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/264] Ugly Numbers & Ugly Number II
https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- Leetcode 264. Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
随机推荐
- 【转】React 常用面试题目与分析
作者:王下邀月熊链接:https://zhuanlan.zhihu.com/p/24856035来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本文有一定概率为水文,怕 ...
- python 之filter()函数
filter()函数是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 f 和一个list,这个函数 f 的作用是对每个元素进行判断,返回 True或 False,filt ...
- bzoj 1611: [Usaco2008 Feb]Meteor Shower流星雨【BFS】
t记录每个格子最早被砸的时间,bfs(x,y,t)表示当前状态为(x,y)格子,时间为t.因为bfs,所以先搜到的t一定小于后搜到的,所以一个格子搜一次就行 #include<iostream& ...
- Head Html Css 第二版笔记
一. 引用 <blockquote>ago aog aogag </blockquote> 则是引用一大段文字并独立显示 二. <a> 创建目的地 <h2&g ...
- Mybatis Generator插件升级版
一.目的: 1. *mapper.java 文件名称 改为*DAO.java2. mapper以及mapper.xml 重复执行,只会覆盖原模板方法,不会覆盖自定义方法3. 实体类添加中文注释 二.步 ...
- java大数轻松过
import java.util.Scanner; import java.math.BigInteger; public class Main { public static void main(S ...
- Kafka~服务端几个常用的命令
在Centos上安装和部署完成kafka之后,我们就可以向服务端推消息和消费它了,今天主要聊几个常用的命令,比建立topic,从broken显示所有topics列表,向broken发消息,从broke ...
- [译]libcurl_tutorial
Handle the Easy libcurl To use the easy interface, you must first create yourself an easy handle. Yo ...
- EasyUI系列学习(十)-Tabs(选项卡)
一.创建组件 <div class="easyui-tabs" style="width:500px;height:250px"> <div ...
- 使用_CRTDBG_LEAK_CHECK_DF检查VC程序的内存泄漏(转)
我们知道,MFC程序如果检测到存在内存泄漏,退出程序的时候会在调试窗口提醒内存泄漏.例如: class CMyApp : public CWinApp{public:BOOL InitApplicat ...