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)的更多相关文章

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

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

  2. (medium)LeetCode 264.Ugly Number II

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

  3. [LeetCode] 264. Ugly Number II 丑陋数之二

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

  4. Leetcode 264. Ugly Number II

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

  5. LeetCode——264. Ugly Number II

    题目: Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime fact ...

  6. 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 ...

  7. 【LeetCode】264. Ugly Number II

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

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

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

  9. 【LeetCode】264. Ugly Number II 解题报告(Java & Python)

    标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ https://leetcode.com/prob ...

随机推荐

  1. Qt5---ftp上传功能(可直接克隆某个小模块,查看QT下FTP的socket原理)

    http://blog.csdn.net/freeape/article/details/52802163

  2. 用了WS_EX_LAYERED 后所有Twincontrl的wm_paint消息会停止(官方Layered Windows文档很多内容)good

    fmx 和 vcl 不一样, fmx 的阴影可以通过2D显示出来. VCL 无标题栏窗口的阴影很麻烦 280425268 我也是用两个窗口做阴影,并重绘了非客户区,不过阴影是基础自TwinContro ...

  3. YxdIocp包含有支持大并发的TCP服务组件、HTTP服务组件、UDP服务组件、WebSocket服务组件

    Delphi Windows IOCP 通讯模型封装,基于DIOCP.YxdIocp包含有支持大并发的TCP服务组件.HTTP服务组件.UDP服务组件.WebSocket服务组件,和TCP.UDP等基 ...

  4. ***R(TCP over UDP,UDP over TCP)

    https://github.com/breakwa11/shadowsocks-rss

  5. 使用docker运行GitLab

    从docker镜像拉取代码,docker pull gitlab/gitlab-ce:latest. 创建/srv/gitlab目录sudo mkdir /srv/gitlab 启动GitLab CE ...

  6. CentOS7.3安装JIRA7.10

    准备工作:下载相关安装包,上传到服务器/opt/apps目录下 链接:https://pan.baidu.com/s/15Y5Y3X6AX2ZokWkZKcRrQQ 密码:q0lw 1.安装数据库 y ...

  7. zabbix2.4汉化

    zabbix的2.4版本安装完后,这里的语言界面选择没有中文,其实是这个版本把中文的屏蔽了. [root@zabbix-server opt]# vim /var/www/html/include/l ...

  8. JAVA String类型的一些小操作

    String类型是否包含某个String类型的函数:源字符串.contains(包含字符串)  返回值为:boolean类型(true或false) String类型把某个字符替换成另一个字符:源字符 ...

  9. 【名额有限】云开发AI拓展能力等你来体验!

    这次来了个超厉害的新能力! 人脸智能打马赛克.人脸智能裁剪--各种操作,都能一步到位! 迫不及待想体验,戳链接:https://wj.qq.com/s2/3986990/e0ef/ 还没有搞懂,继续往 ...

  10. HDU 2121:Ice_cream’s world II(不定根的最小树形图)

    题目链接 题意 求有向图的最小生成树,且根不定. 思路 最小树形图即求有向图的最小生成树,用的是朱刘算法. 这里不定根,那么可以建立一个虚根,让虚根和所有点相连,权值为一个很大的数(这里直接设为所有边 ...