Leetcode264. Ugly Number II丑数2
编写一个程序,找出第 n 个丑数。
丑数就是只包含质因数 2, 3, 5 的正整数。
示例:
输入: n = 10 输出: 12 解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 10 个丑数。
说明:
- 1 是丑数。
- 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的更多相关文章
- 264 Ugly Number II 丑数 II
编写程序找第 n 个丑数.丑数就是只包含质因子 2, 3, 5 的正整数.例如, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 就是前10个丑数.注意:1. 1 一般也被当做丑数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 ...
- LeetCode OJ:Ugly Number(丑数)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- [LeetCode] 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]313. Super Ugly Number超级丑数,丑数系列看这一道就行了
丑数系列的题看这一道就可以了 /* 和ugly number2差不多,不过这次的质因子多了,所以用数组来表示质因子的target坐标 target坐标指的是这个质因子此次要乘的前任丑数是谁 */ pu ...
- 【easy】263. Ugly Number 判断丑数
class Solution { public: bool isUgly(int num) { ) return false; ) return true; && num % == ) ...
- LeetCode OJ 之 Ugly Number (丑数)
题目: Write a program to check whether a given number is an ugly number. Ugly numbers are positive num ...
- 313 Super Ugly Number 超级丑数
编写一段程序来寻找第 n 个超级丑数.超级丑数是指其所有质因数都在长度为k的质数列表primes中的正整数.例如,[1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] ...
随机推荐
- <每日一题>题目30:已知一个长度n的无序列表,元素均是数字,要求把所有间隔为d的组合找出来
def select_d(list,d): # list = sorted(list) sum = {} for i in list: if i+d in list: sum[i] = i+d ret ...
- printk 函数消息是如何记录的
printk 函数将消息写入一个 LOG_BUF_LEN 字节长的环形缓存, 长度值从 4 KB 到 1 MB, 由配置内核时选择. 这个函数接着唤醒任何在等待消息的进程, 就是说, 任何在系统 ...
- css实现截取文本
.ellipsis{ max-width: 260px; // 自定义 overflow: hidden; text-overflow: ellipsis; white-space: nowrap; ...
- 杜教筛&套路总结
杜教筛 \[ \begin{split} (g*f)(i)&=\sum_{d|i}g(d)f(\frac id)\\ \Rightarrow g(1)S(n)&=\sum_{i=1}^ ...
- Clion IDE的安装
/下载压缩包 wget https://download.jetbrains.8686c.com/cpp/CLion-2016.2.2.tar.gz //解压 tar -zxvf CLion-2016 ...
- 莫烦pytorch学习笔记(七)——Optimizer优化器
各种优化器的比较 莫烦的对各种优化通俗理解的视频 import torch import torch.utils.data as Data import torch.nn.functional as ...
- apache配置域名子目录,访问不同子项目
<VirtualHost *:443> DocumentRoot "E:/phpstudy/WWW/nextplus" ServerName local-main.co ...
- Vue.js项目部署到服务器
1.申请服务器 2.配置Xshell 3.在服务器手动建自己的根目录,把根目录的文件名复制给项目里面config下面的index.js 4.项目开始打包 npm run build 5.打包完成之后把 ...
- System.Text.Encoding.cs
ylbtech-System.Text.Encoding.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77 ...
- Android基础控件ListView基础操作
1.简介 基于Android基础控件ListView和自定义BaseAdapter适配器情况下,对ListView的数据删除和添加操作: public boolean add(E e) {//添加数据 ...