题目:

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.

思路:

參考:http://blog.csdn.net/u012243115/article/details/45222269

代码:

class Solution {
public:
int nthUglyNumber(int n)
{
if(n <= 0)
return 0;
int *uglyNum = new int[n]();
int *uglyNum2 = uglyNum ;
int *uglyNum3 = uglyNum ;
int *uglyNum5 = uglyNum ;
uglyNum[0] = 1;
int count = 1;
while(count < n)
{
int curUgly = min(min(*uglyNum2 * 2 , *uglyNum3 * 3) , *uglyNum5 * 5);
uglyNum[count] = curUgly;
while(*uglyNum2 * 2 <= curUgly)
uglyNum2++;
while(*uglyNum3 * 3 <= curUgly)
uglyNum3++;
while(*uglyNum5 * 5 <= curUgly)
uglyNum5++;
count++;
}
int result = uglyNum[n-1];
delete [] uglyNum;
return result; }
};

LeetCode OJ 之 Ugly Number II (丑数-二)的更多相关文章

  1. LeetCode OJ:Ugly Number(丑数)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  2. LeetCode OJ 之 Ugly Number (丑数)

    题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...

  3. LeetCode OJ:Ugly Number II(丑数II)

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

  4. 264 Ugly Number II 丑数 II

    编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...

  5. [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了

    丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...

  6. Leetcode264. Ugly Number II丑数2

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

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

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

  8. [LeetCode] 313. Super Ugly Number 超级丑陋数

    Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...

  9. 【刷题-LeetCode】264. Ugly Number II

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

随机推荐

  1. Python爬虫入门:URLError异常处理

    大家好,本节在这里主要说的是URLError还有HTTPError,以及对它们的一些处理. 1.URLError 首先解释下URLError可能产生的原因: 网络无连接,即本机无法上网 连接不到特定的 ...

  2. 聊聊keep-alive组件的使用及其实现原理

    写在前面 因为对Vue.js很感兴趣,而且平时工作的技术栈也是Vue.js,这几个月花了些时间研究学习了一下Vue.js源码,并做了总结与输出. 文章的原地址:https://github.com/a ...

  3. 29.使用register_chrdev_region()系列来注册字符设备

    1.之前注册字符设备用的如下函数注册字符设备驱动: register_chrdev(unsigned int major, const char *name,const struct file_ope ...

  4. 结合GET(),POST()实现一个简单、完整的服务器

    复习一下: 基础模块 作用 fs fs模块用于对系统文件及目录进行读写操作 http 创建服务器.e.g.http.createServer(); queryString 把url带的参数串转化为数组 ...

  5. [转载] Redis实现分布式锁

    转载自http://zhidao.baidu.com/link?url=m56mmWYwRgCymsaLZ2tx-GWDy5FYmUWGovEtuApjTpktHS3bhofrCS-QVGiLoWeS ...

  6. github设置

    ssh-key: https://help.github.com/articles/generating-ssh-keys http://segmentfault.com/q/101000000013 ...

  7. Nytro MegaRaid

    Nytro MegaRaid简介 Dell R720xd,内存64G ,12块 SAS Dell R510xd,内存48G ,12块 SAS   SSD+SAS   SSD对于用户透明   raid会 ...

  8. 使用JavaScript生成二维码教程-附qrcodejs中文文档

    使用javascript生成二维码 依赖jquery 需要使用到的库 https://github.com/davidshimjs/qrcodejs DIV <div id="qrco ...

  9. Maven启动Java Web工程,8081和8086端口号被占用

    Maven启动Java Web工程, <!-- 配置tomcat插件 --> <build> <plugins> <plugin> <groupI ...

  10. python win32 简单操作

    源由 刚开始是帮朋友做一个按键精灵操作旺信的脚本,写完后各种不稳定:后来看到python可以操作win32相关的api,恰好这一段时间正在学习python,感觉练手的时候到了~~~ 下载 要注意Pyt ...