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 坚持
开玩笑要有个度,哎,或许这就是缘分,很容易受别人影响吗? 中国人为什么会经常抱怨,不抱怨,挺好. 睡觉,红颜祸水,老婆是要能一起 生活的,不是失去,是上天在帮我,哈哈.
- 现代程序设计 homework-05
本次作业要求设计服务器和客户端,由于之前对网络编程是一窍不通,上上节课听宗学长讲述Tcp的时候心里想这个东西还真是高大上啊一点儿都听不懂,但是上个周末看了看C#网络编程的博客和书之后,发现这个东西入门 ...
- HDU 5781 ATM Mechine (概率DP)
ATM Mechine 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5781 Description Alice is going to take ...
- Spring 使用注解方式进行事物管理
大家在使用spring的注解式事务管理时,对事务的传播行为和隔离级别可能有点不知所措,下边就详细的介绍下以备方便查阅. 事物注解方式: @Transactional 当标于类前时, 标示类中所有方法都 ...
- 允许ubuntu下mysql远程连接
第一步: gedit /etc/mysql/my.cnf找到bind-address = 127.0.0.1 注释掉这行,如:#bind-address = 127.0.0.1 或者改为: bind- ...
- Myeclipse 10.x 安装Aptana3.2 插件
安装步骤: 1.下载aptana3.2 Eclipse Plugin插件. 下载地址:http://update1.aptana.org/studio/3.2/024747/index.html 2. ...
- window.open()打开窗口的几种方式
1. window.open("http://www.baidu.com/", "_search");//在一个新的窗口打开百度,并且使URL地址出现在搜索栏中 ...
- MongoDB 快速入门--初级
数据库的操作一般来说都是CRUD,这其中最难的就是查询,所有所我们先来了解MongoDB中的 插入(insert) 说到插入,我们就必须得说说如何创建数据库,如何创建集合,然后才是如何创建文档. 在这 ...
- 安装centreon
Centreon + nagios + ndoutils 安装 2013-09-25 19:42:44 标签:centreon 原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者 ...
- bash学习之变量的显示和设置
显示变量:echo $MAIL或者 echo ${MAIL} [CJP@CJP ~]$ echo $MAIL /var/spool/mail/CJP [CJP@CJP ~]$ echo ${MAIL} ...