[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 ...
随机推荐
- Delphi下IOC 模式的实现(反转模式,即Callback模式)
IOC英文为 Inversion of Control,即反转模式,这里有著名的好莱坞理论:你呆着别动,到时我会找你.Ioc模式是解决调用者和被调用者之间关系的模式,可以有效降低软件的耦合度,并适合团 ...
- Qt for Android之Hello World
Qt for Android (Hello World APK 创建)Qt是跨平台的,如桌面.移动.嵌入式平台.Qt for Android可以在Android v2.3.3 (API level 1 ...
- Python连载16-reduce函数&filter函数
一.reduce函数 本函数释义: (1)原意是归并,缩减 (2)把一个可迭代的对象最后归并成一个结果 (3)对于作为参数的函数要求:必须由两个参数,必须返回一个结果 import functools ...
- DHCP命令执行CVE-2018-1111漏洞复现
DHCP命令执行_CVE-2018-1111漏洞复现 一.漏洞描述 在Red Hat Enterprise Linux多个版本的DHCP客户端软件包所包含的NetworkManager集成脚本中发现了 ...
- 05 div的嵌套
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Spark学习之路(十四)—— Spark Streaming 基本操作
一.案例引入 这里先引入一个基本的案例来演示流的创建:获取指定端口上的数据并进行词频统计.项目依赖和代码实现如下: <dependency> <groupId>org.apac ...
- 【分页工具-spring boot】Mybatis PageHelper 集成Spring boot
官方文档:https://github.com/pagehelper/pagehelper-spring-boot 1.引入包,测试过以下版本兼容性还是比较好的 <!--Mybatis-Spri ...
- 手动搭建自己的nuget服务器及使用
这篇文章的主要目的: 1.搭建自己的私有的nuget服务器 2.打包代码为nuget包 3.在其他项目中使用私有服务器上的nuget包 一. 搭建自己的nuget服务器 1. 创建一个空的ASP.NE ...
- 模块化 require.js 入门教学(前端必看系列)
在工作的时候总是会用到模块化开发,那接下来我就顺着这个问题来说一下什么是模块化 前端模块化 !!! JS 模块化提供给我们三种规范 分别就是 No.1 commonjs 这个其实也就代表了node. ...
- vs 编译说明
静态编译/MT,/MTD 是指使用libc和msvc相关的静态库(lib). 动态编译,/MD,/MDd是指用相应的DLL版本编译. 其中字母含义 d:debug m:multi-th ...