LeetCode 264
Ugly Number II
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.
Hint:
The naive approach is to call isUgly for every number until you
reach the nth one. Most numbers are not ugly.
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).
/*************************************************************************
> File Name: LeetCode264.c
> Author: Juntaran
> Mail: JuntaranMail@gmail.com
> Created Time: Wed 18 May 2016 20:12:58 PM CST
************************************************************************/ /************************************************************************* Ugly Number II 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. Hint: The naive approach is to call isUgly for every number until you
reach the nth one. Most numbers are not ugly.
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). ************************************************************************/ int minofThree( int a, int b, int c )
{
int min = a>b ? b : a;
min = min>c ? c : min;
return min;
} int nthUglyNumber(int n)
{
if( n <= )
return ;
if(n == )
return ;
int *uglyArr = (int*)malloc(sizeof(int)*n);
int i2=, i3=, i5=;
int i;
uglyArr[] = ; for( i=; i<n; i++ )
{
int t2 = uglyArr[i2]*;
int t3 = uglyArr[i3]*;
int t5 = uglyArr[i5]*;
int min = minofThree( t2, t3, t5 );
uglyArr[i] = min;
if(min == t2)
i2++;
if(min == t3)
i3++;
if(min == t5)
i5++;
}
return uglyArr[n-];
} int main()
{
int n = ;
int ret = nthUglyNumber(n);
printf("%d\n", ret);
}
LeetCode 264的更多相关文章
- [LeetCode] 264. 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 丑陋数 II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode——264. 丑数 II
编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...
- Java实现 LeetCode 264 丑数 II(二)
264. 丑数 II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, ...
- leetcode 264. 丑数 II 及 313. 超级丑数
264. 丑数 II 题目描述 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, ...
- Leetcode 264. Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- (medium)LeetCode 264.Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- Leetcode 264.丑数II
丑数II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10 ...
- [leetcode] 264. Ugly Number II (medium)
263. Ugly Number的子母题 题目要求输出从1开始数,第n个ugly number是什么并且输出. 一开始想着1遍历到n直接判断,超时了. class Solution { public: ...
随机推荐
- 第二百九十、一、二天 how can I 坚持
上周日,腊八,好冷,去菏泽了.坐火车,没座.好冷,下午菏泽学院,大体看了看,很是怀念,有点遗憾,在网吧玩的时间太长了,留不住. 周一,长太婚礼,竟然没有只是吃了个饭,有点小失望,下午坐车回京,两点坐的 ...
- Android View事件传递机制
ViewGroup dispatchTouchEvent onInterceptTouchEvent onTouch View dispatchTouchEvent onTouch 假设View的层级 ...
- linux sar 命令详解
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告,包括:文件的读写情况.系统调用的使用情 ...
- 转载 SQL Server中索引管理之六大铁律
转载原地址 http://jingyan.baidu.com/article/48a42057c03bd7a924250429.html 索引是以表列为基础的数据库对象.索引中保存着表中排序的索引列, ...
- iOS开发60分钟入门
原文:https://github.com/qinjx/30min_guides/blob/master/ios.md 本文面向已有其它语言(如Java,C,PHP,Javascript)编程经验的i ...
- AOV网络拓扑排序
这个算法,主要是为输出一个无环图的拓扑序列 算法思想: 主要依赖一个栈,用来存放没有入度的节点,每次读取栈顶元素,并将栈顶元素的后继节点入度减一,如果再次出现入度为零的节点,就加入到栈中.参考< ...
- 剑指OFFER之二进制中1的个数(九度OJ1513)
题目描述: 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 输入: 输入可能包含多个测试样例.对于每个输入文件,第一行输入一个整数T,代表测试样例的数量.对于每个测试样例输入为一个整 ...
- C# 上传excel文档解析出里面数据
if (fileExt.ToUpper() == ".XLS" || fileExt.ToUpper() == ".XLSX" || fileExt.ToUpp ...
- 汇编语言(学习笔记----寄存器CPU互作原理)
一.段寄存器 1.段寄存器就是提供段地址的,8086CPU有4个段寄存器:CS(代码段寄存器),DS(数据段寄存器),SS(堆栈段寄存器),ES(附加段寄存器) 2.当8086CPU要访问内存时,由这 ...
- Weka – 分类
1. weka简单介绍 1) weka是新西兰怀卡托大学WEKA小组用JAVA开发的机器学习/数据挖掘开源软件. 2) 相关资源链接 http://sourceforge.net/pro ...