[leetcode] 264. Ugly Number II (medium)
263. Ugly Number的子母题
题目要求输出从1开始数,第n个ugly number是什么并且输出。
一开始想着1遍历到n直接判断,超时了。
class Solution
{
public:
bool isUgly(int num)
{
while (num % == && num > )
num /= ;
while (num % == && num > )
num /= ;
while (num % == && num > )
num /= ;
return num == ;
}
int nthUglyNumber(int n)
{
int res = ;
vector<int> sta;
sta.push_back(res);
while (sta.size() <= n)
{
++res;
if (isUgly(res))
sta.push_back(res);
else
{
while (!isUgly(res))
{
++res;
}
sta.push_back(res);
}
}
for (auto a : sta)
{
cout << a;
}
return sta[n];
}
};
超时以后想通过数组保存ugly数字,然后对其排序,直接输出第n个ugly数字。
这里有点投机取巧的意思。利用static去保存,这样在不同数据测试中,只需要第一次计算保存数据,后面只需要执行返回static里面的值就好了,所以评测结果非常快。
Runtime: 4 ms, faster than 97.70% of C++ online submissions for Ugly Number II.
class Solution
{
public:
int nthUglyNumber(int n)
{
static vector<int> uglyNums;
long long a, b, c, maxN = INT_MAX;
if (uglyNums.empty())
{
for (a = 1; a < maxN; a *= 2)
for (b = a; b < maxN; b *= 3)
for (c = b; c < maxN; c *= 5)
uglyNums.push_back(c);
sort(begin(uglyNums), end(uglyNums));
}
return uglyNums[n - 1];
}
};
最优解里看到的,也是讨论区里面用的最多的一种方法,0ms。
class Solution
{
public:
int nthUglyNumber(int n) {
static vector<int> ugly {};
static int last();
static int c2=, c3=, c5=;
static int i2=, i3=, i5=;
while (ugly.size() < n) {
while (c2 <= last) c2 = * ugly[++i2];
while (c3 <= last) c3 = * ugly[++i3];
while (c5 <= last) c5 = * ugly[++i5];
ugly.push_back(last = min(c2, min(c3, c5)));
}
return ugly[n-];
}
};
[leetcode] 264. Ugly Number II (medium)的更多相关文章
- [LeetCode] 264. Ugly Number II 丑陋数 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. 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
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode——264. Ugly Number II
题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...
- leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
263. Ugly Number 注意:1.小于等于0都不属于丑数 2.while循环的判断不是num >= 0, 而是能被2 .3.5整除,即能被整除才去除这些数 class Solution ...
- 【LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【刷题-LeetCode】264. Ugly Number II
Ugly Number II Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose ...
- 【LeetCode】264. Ugly Number II 解题报告(Java & Python)
标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...
随机推荐
- 关于跨进程使用回调函数的研究:以跨进程获取Richedit中RTF流为例(在Delphi 初始化每一个TWinControl 对象时,将会在窗体 的属性(PropData)中加入一些标志,DLL的HInstance的值与HOST 进程的HInstance并不一致)
建议先参考我上次写的博文跨进程获取Richedit中Text: 获得QQ聊天输入框中的内容 拿到这个问题,我习惯性地会从VCL内核开始分析.找到TRichEdit声明的单元,分析TRichEdit保存 ...
- Silverlight消散,WinRT登台
2011年,Silverlight刚开始有蓬勃发展的起色,不利的传言就开始大量流传.不安的Silverlight开发者们要求微软澄清,但得到的只是沉默.终于随着微软在BUILD上亮相Window 8以 ...
- 使用VS2010再装VS2013不用再烦恼不兼容
某些同事有时在开发过程中出现这么个问题,在使用js直接异步调用类库时,弹出错误类库不存在或者没有定义等,类似问题,这个时候可能你正在绞尽脑汁的去解决问题,明明问题不大,为什么安装VS2013后就不能打 ...
- ansible(二)
一.软件相关模块 1.yum(下载包) 正常操作 yum 与rpm的区别 yum可以解决依赖关系rpm 全称readhat package manager(红帽包管理工具),需要自己解决依赖 yum源 ...
- 优秀的Restful API应该是什么样的
1 你一直在错误的使用http协议 现在微服务真是火的一塌糊涂!大街小巷,逢人必谈微服务,各路大神纷纷忙着把自家的单体服务拆解成多个Web微小服务!而作为微服务之间通信的桥梁,Web API的设计就显 ...
- ABP开发框架前后端开发系列---(4)Web API调用类的封装和使用
在前面随笔介绍ABP应用框架的项目组织情况,以及项目中领域层各个类代码组织,以及简化了ABP框架的各个层的内容,使得我们项目结构更加清晰.上篇随笔已经介绍了字典模块中应用服务层接口的实现情况,并且通过 ...
- hgoi#20190513
T1-Felicity is Coming! 神奇宝贝的进化方案是一个全排列,假设有三种宝可梦,那么对应就可以有: (1,2,3)(1,3,2)(2,1,3)(2,3,1)(3,1,2)(3,2,1) ...
- Spring源码解读之BeanFactoryPostProcessor的处理
前言 前段时间旁听了某课堂两节Spring源码解析课,刚好最近自己又在重新学习中,便在这里记录一下学习所得.我之前写过一篇博文,是介绍BeanFactoryPostProcessor跟BeanPost ...
- C#常用设计模式--单例模式
为什么要使用单例模式 在我们的整个游戏生命周期当中,有很多对象从始至终有且只有一个.这个唯一的实例只需要生成一次,并且直到游戏结束才需要销毁. 单例模式一般应用于管理器类,或者是一些需要持久化存在的 ...
- chrome如何查看cookie
以mac为例: 第一步:点击chrome的偏好设置 第二步:点击如下图所示的最下面的高级 第三步:点击内容设置,如下所示 第四步:点击cookie,就会出现查看所有cookie和网站数据