LeetCode OJ 之 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, is the sequence of the first
2, 3, 4, 5, 6, 8, 9, 10, 1210 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 (丑数-二)的更多相关文章
- LeetCode OJ:Ugly Number(丑数)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- LeetCode OJ 之 Ugly Number (丑数)
题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...
- LeetCode OJ:Ugly Number II(丑数II)
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- 264 Ugly Number II 丑数 II
编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...
- [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了
丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...
- Leetcode264. Ugly Number II丑数2
编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 ...
- [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] 313. Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- 【刷题-LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
随机推荐
- asp.net 自定义的模板方法接口通用类型
本来想写这个帖子已经很久了,但是公司事情多,做着做着就忘记了.公司因为需要做接口,而且用的还是asp.net的老框架,使用Handler来做,没得办法,自己照着MVC写了一个通过的接口操作模板. 上送 ...
- Python 面向对象基础知识
面向对象基础知识 1.什么是面向对象编程? - 以前使用函数 - 类 + 对象 2.什么是类什么是对象,又有什么关系? class 类: def 函数1(): pass def 函数2(): pass ...
- python20171113笔记
问题一: python {File "<stdin>", line 1} error 解决方法:就是不用先输入 python进入python解释器,而直接输入pytho ...
- Java笔试题解答
1. 下面哪些是Thread类的方法() A start() B run() C exit() D getPriority() 答案:ABD 解析:看Java AP ...
- [转载] Redis之七种武器
转载自http://blog.nosqlfan.com/html/2942.html?ref=rediszt 长生剑.孔雀翎.碧玉刀.多情环.离别钩.霸王枪.拳头是古龙笔下的七种武器,而本文打算将Re ...
- TweenMax动画库学习
之前在做HTML5移动端开发的时候,用的都是Animate.css,这个插件封装的的确很好,但是在做一些缓动方面的动画,它也有一定的不足之处,比如手要写一个连续的动画,需要不停的去重复写函数,使得代码 ...
- github设置
ssh-key: https://help.github.com/articles/generating-ssh-keys http://segmentfault.com/q/101000000013 ...
- Go环境搭建
Linux系统golang环境搭建 1.下载安装包go1.8.linux-amd64.tar golang安装包下载地址:https://golang.org/dl/ ( 有可能被FQ) 2.解 ...
- Python 日志处理(二) 使用正则表达式处理Nginx 日志
使用正则表达式来处理Nginx 日志 一. 先对单行的日志进行分组正则匹配,返回匹配后的结果(字典格式): from datetime import datetime import re #单行日志 ...
- 初试pyspider
灵感来源: https://zhuanlan.zhihu.com/p/31421316 抓取页面: https://www.nvshens.com/tag/new/ 页面分析: 首页获取所有图片详情页 ...