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写了一个通过的接口操作模板. 上送 ...
- Jxl创建Excel文件和解析Excel文件
import java.io.File; import jxl.Workbook; import jxl.write.Label; import jxl.write.WritableSheet; im ...
- 【原创】使用workstation安装Xenserver 6.5+cloudstack 4.10----本地存储模式
1. 背景: 近期由于项目和个人学习得需求,开始接触到Cloudstack,虽然云计算概念在大学刚毕业的时候就已经略有耳闻,但是由于工作原因,也一直没有了解,下班后想自己折腾下cloudstack,便 ...
- 【手记】让Fiddler抓取入站请求,或者叫用Fiddler做反向代理
注意:本文不涉及HTTPS的场景 最近在弄公众号开发,除了主动去调公众号接口,还存在公众号后台要反过来调你的情形,攻受转换一线间.对于回调的情况,想要知道对方是怎样来请求的很有必要.此前经常用Fidd ...
- PAM - 可插拔认证模块
1.为什么要使用PAM 为了让用户更合理地使用系统,应用程序或服务(如sshd.login.su.password.ftp等)不可避免地需要对用户进行安全认证,若按照各自的规则去配置非常耗费时间和精力 ...
- css3实现聊天气泡
1: <div class="comment"></div><style type="text/css"> .comment ...
- Transform组件以及相关API
Transform.Translate(Vector3,Space):以自身坐标系或者世界坐标系向某个方向移动物体. Vector3:表示方向和移动的距离. Space:空间.枚举类型,用来确定坐标系 ...
- 一个页面tab标签切换,都有scroll事件的解决办法
当前页有多个tab,如果都有scroll事件, 先解绑$(window).off('scroll') 再执行scroll就不可以了,多个标签就不会互相干扰: 给你们个例子: //标签切换 $(' ...
- 网页的居中显示,使用了margin、clear:both
很久没写过页面了,现在写起来也觉得捡起来还是挺快的. 当时遇到了这样的问题,我有一个大的div包涵了整个网站,有网页头部,中部还有底部.头部就是一个标题,中部就是几张图片跟文字排版,结果左右两边的图片 ...
- Pyhton函数篇(一)之函数中的形参与实参
1:什么是函数 函数其实就是带名字的代码块,用于完成一些具体的工作.如果我们在写一段程序的时候,需要多次用到同样的一个功能,如果每次都要重复写相同的代码,不仅会增加我们的代码量,更会让我们写出的代码让 ...