编写一个程序,找出第 n 个丑数。

丑数就是只包含质因数 2, 3, 5 的正整数。

示例:

输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。

说明:

  1. 1 是丑数。
  2. n 不超过1690。

不用else if的原因是为了去重

class Solution {
public:
int nthUglyNumber(int n)
{
int three = 0;
int two = 0;
int five = 0;
vector<int> res(n, 0);
res[0] = 1;
for(int i = 1; i < n; i++)
{
res[i] = min(res[two] * 2, min(res[three] * 3, res[five] * 5));
if(res[i] == res[two] * 2)
{
two++;
}
if(res[i] == res[three] * 3)
{
three++;
}
if(res[i] == res[five] * 5)
{
five++;
}
}
return res[n - 1];
}
};

Leetcode264. Ugly Number II丑数2的更多相关文章

  1. 264 Ugly Number II 丑数 II

    编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数2. ...

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

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

  3. LeetCode OJ:Ugly Number(丑数)

    Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...

  4. [LeetCode] 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 factors ...

  6. [LeetCode]313. Super Ugly Number超级丑数,丑数系列看这一道就行了

    丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...

  7. 【easy】263. Ugly Number 判断丑数

    class Solution { public: bool isUgly(int num) { ) return false; ) return true; && num % == ) ...

  8. LeetCode OJ 之 Ugly Number (丑数)

    题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...

  9. 313 Super Ugly Number 超级丑数

    编写一段程序来寻找第 n 个超级丑数.超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数.例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] ...

随机推荐

  1. css盒模型问题

    css盒模型问题 1.基本概念:标准模型和ie模型 2.标准模型和ie模型的区别 3.css如果设置这两种模型 4.js如何获取盒模型的宽高 5.边距重叠 6.BFC 1.CSS盒模型本质上是一个盒子 ...

  2. grep 强大的文本搜索工具

    1.grep -r "History folder does't exist:" * :中间是要搜索的文本,* 表示全部显示出来

  3. [转]NuGet学习笔记(1) 初识NuGet及快速安装使用

    关于NuGet园子里已经有不少介绍及使用经验,本文仅作为自己研究学习NuGet一个记录. 初次认识NuGet是在去年把项目升级为MVC3的时候,当时看到工具菜单多一项Library Package M ...

  4. data hazard in CPU pipeline

    1, background info 5 stages in CPU pipeline: IF, ID, EX, MM, WB IF – Instruction Fetch ID – Instruct ...

  5. CentOS7使用firewalld打开关闭防火墙与端口(转)

    CentOS7使用firewalld打开关闭防火墙与端口       1.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop ...

  6. win10系统,vbox下安装centos6/7,挂载实现目录共享

    用下载好的iso文件,新建虚拟机(所有步骤默认下一步即可). 我用的centos版本:CentOS-7-x86_64-Minimal-1908.iso ISO下载地址 设置虚拟机(指定好镜像后,先不要 ...

  7. JS中的Map和Set

    使用iterable内置的forEach方法 var a = ['A', 'B', 'C']; a.forEach(function (element, index, array) { // elem ...

  8. Bitcoin 的基本原理

    昨天读到了 Bitcoin 的中文介绍,觉得非常有意思.不过上面这篇文章解释的非常不靠谱,我花了一晚上去Bitcoin的官方网站 仔细研究了一下,总算理解了其原理.感觉非常有启发,尤其是对虚拟货币的流 ...

  9. PAT甲级——A1007 Maximum Subsequence Sum

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  10. centos7 yum 安装tomcat7

    查看yum中tomcat信息 yum info tomcat 安装 yum install tomcat 安装管理界面 yum install tomcat-webapps tomcat-admin- ...