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. Spark Streaming 原理剖析

    通过源码呈现 Spark Streaming 的底层机制. 1. 初始化与接收数据 Spark Streaming 通过分布在各个节点上的接收器,缓存接收到的流数据,并将流数 据 包 装 成 Spar ...

  2. Android实例-Delphi开发蓝牙官方实例解析(XE10+小米2+小米5)

    相关资料:1.http://blog.csdn.net/laorenshen/article/details/411498032.http://www.cnblogs.com/findumars/p/ ...

  3. HDU 5805 NanoApe Loves Sequence (模拟)

    NanoApe Loves Sequence 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5805 Description NanoApe, the ...

  4. Spring Auto proxy creator example

    In last Spring AOP examples – advice, pointcut and advisor, you have to manually create a proxy bean ...

  5. POJ 3671 Dining Cows (DP,LIS, 暴力)

    题意:给定 n 个数,让你修改最少的数,使得这是一个不下降序列. 析:和3670一思路,就是一个LIS,也可以直接暴力,因为只有两个数,所以可以枚举在哪分界,左边是1,右边是2,更新答案. 代码如下: ...

  6. Unity3D之空间转换学习笔记(二):基础数学

    这期笔记我们专注Unity提供的各种数学相关的类来学习. 时间Time API文档地址:http://docs.unity3d.com/ScriptReference/Time.html 时间加/减速 ...

  7. presentedViewController 和 presentingViewController 以及 dismissViewControllerAnimated 的使用

    在日常的开发中,多控制器之间的跳转除了使用push的方式,还可以使用 present的方式,present控制器时,就避免不了使用 presentedViewController.presenting ...

  8. List、ArrayList、Vector及map、HashTable、HashMap分别的区别

    一.List与ArrayList的区别      List->AbstractList->ArrayList     (1) List是一个接口,ArrayList是一个实现了List接口 ...

  9. apache win openssl

    Rubayat Hasan Software Development, Music, Web Design, life, thoughts…   Home Portfolio Projects Con ...

  10. 亦步亦趋在CentOS 6.4下安装Oracle 11gR2(x64)

    安装前须知: 内存(RAM)的最小要求是 1GB,建议 2GB 及以上. 虚拟内存 swap 建议:内存为 1GB~2GB 时建议swap大小为内存大小的 1.5 倍:内存为 2GB~16GB 时建议 ...