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的更多相关文章

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

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

  2. [LeetCode] 264. Ugly Number II 丑陋数 II

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

  3. LeetCode——264. 丑数 II

    编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...

  4. Java实现 LeetCode 264 丑数 II(二)

    264. 丑数 II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, ...

  5. leetcode 264. 丑数 II 及 313. 超级丑数

    264. 丑数 II 题目描述 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, ...

  6. Leetcode 264. Ugly Number II

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

  7. (medium)LeetCode 264.Ugly Number II

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

  8. Leetcode 264.丑数II

    丑数II 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10 ...

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

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

随机推荐

  1. STL学习系列五:Queue容器

    Queue简介 queue是队列容器,是一种“先进先出”的容器. queue是简单地装饰deque容器而成为另外的一种容器. #include <queue> 1.queue对象的默认构造 ...

  2. nyoj 120 校园网络(求添加多少条边使整个图强连通)

    校园网络 时间限制:3000 ms  |  内存限制:65535 KB 难度:5   描述 南阳理工学院共有M个系,分别编号1~M,其中各个系之间达成有一定的协议,如果某系有新软件可用时,该系将允许一 ...

  3. 当心回车符破坏你的JSON数据

    今天发现系统中一个地方获取JSON数据时,时而失败,时而成功,最后发现是回车符搞的鬼. 当你的JSON中有回车符时,会致使你的JSON出现格式错误:解决办法是在保存数据,或整理数据向客户端输出时将回车 ...

  4. mvc annotation-driven作用

    会自动注册DefaultAnnotationHandlerMapping与AnnotationMethodHandlerAdapter两个bean,是spring MVC为Controller分发请求 ...

  5. iOS中多控制器的使用

    通常情况下,一个app由多个控制器组成,当app中有多个控制器的时候,我们就需要对这些控制器进行管理. 在开发过程中,当有多个View时,可以用一个大的view去管理多个小的view,控制器也是如此, ...

  6. sql2008“备份集中的数据库备份与现有数据库不同”解决方法

    因为是在另一台电脑对同名数据库做的备份,用常规方法还原,提示不是相同数据库,不让还原,在网上找到下面的方法解决了: 一.右击系统数据库master,新建查询 执行以下SQL代码: RESTORE DA ...

  7. Hadoop 2.0+YARN启动脚本分析与实战经验

    start-all.sh脚本现在已经废弃,推荐使用start-dfs.sh和start-yarn.sh分别启动HDFS和YARN. 在新一代的Hadoop里面HDFS称为了统一存储的平台,而YARN成 ...

  8. 目录启动CXF启动报告LinkageError异常以及Java的endorsed机制

    本文纯属个人见解,是对前面学习的总结,如有描述不正确的地方还请高手指正~ Exception in thread "main" java.lang.LinkageError: JA ...

  9. cdoj 1256 昊昊爱运动 预处理/前缀和

    昊昊爱运动 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1256 Descr ...

  10. 1039. Course List for Student (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1039 题目: 1039. Course List for Student (25) 时间限制 2 ...