[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
isUglyfor 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 ...
随机推荐
- Bootstrap4 网格系统
学习注意事项 col-*-* 第一个*是设备类型,第二个*是控件宽度的占比 屏幕被等分为12,col-1宽度是1/12,col-6宽度是50%,col-12宽度是100% 给应用了class的elem ...
- luogu 3951 小凯的疑惑
noip2017 D1T1 小凯的疑惑 某zz选手没有看出这道结论题,同时写出了exgcd却不会用,只能打一个哈希表骗了30分 题目大意: 两个互质的正整数a和b,求一个最小的正整数使这个数无法表示为 ...
- Linux下 FACL详解
1. 什么是ACL ACL即Access Control List 主要的目的是提供传统的owner,group,others的read,write,execute权限之外的具体权限设置 ACL可以针 ...
- Python基础类型(一) int 整型
Python算术运算符 以下假设变量: a=10,b=20: 运算符 描述 实例 + 加 - 两个对象相加 a + b 输出结果 30 - 减 - 得到负数或是一个数减去另一个数 a - b 输出结果 ...
- 【原创】Eclipse实现图形化界面插件-vs4e
vs4e插件下载地址:http://visualswing4eclipse.googlecode.com/files/vs4e_0.9.12.I20090527-2200.zip 下载完成后,解压,然 ...
- oozie timezone时区配置
cloudera oozie默认时区是UTC,在开发oozie任务时必须在期望执行的时间上减去8小时,很不习惯.记录下修改时区的配置操作. 1. cloudera oozie配置—>Oozie ...
- magento “Model collection resource name is not defined” 错误
问题出现于使用Grid时,解决方案.在使用的Model处添加 public function _construct() { parent::_construct(); $this->_init( ...
- Asp 循环输出 form 表单提交的数据
亲测asp提交form表单数据,在接收页面循环输出数据 dim var for each var in request.form response.write var&"=" ...
- 3122 奶牛代理商 VIII
3122 奶牛代理商 VIII 时间限制: 3 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 小徐是USACO中国区的奶牛代理商, ...
- tp在页面输出时间
输出时间戳 :{:time()} 输出当前时间:{:date('Y-m-d H:i:s')} 输出1970的时间:{:date('Y-m-d H:i:s',$vo['create_time'])}