当前位置:网站首页>剑指 Offer 49. 丑数(三指针法)
剑指 Offer 49. 丑数(三指针法)
2022-06-28 01:47:00 【BugMaker-shen】
一、263. 丑数

class Solution {
public:
bool isUgly(int n) {
if(n <= 0){
return false;
}
while(n % 2 == 0){
n >>= 1;
}
while(n % 3 == 0){
n /= 3;
}
while(n % 5 == 0){
n /= 5;
}
return n == 1;
}
};
二、264. 丑数 II

1. 三指针法

class Solution {
public:
int nthUglyNumber(int n) {
int idx2 = 0;
int idx3 = 0;
int idx5 = 0;
vector<int> nums(n, 0);
nums[0] = 1; // 第一个丑数为1
for(int i = 1; i < n; i++){
// 由数组里的丑数生成新的丑数,将最小的丑数存入数组
nums[i] = min(nums[idx2] * 2, min(nums[idx3] * 3, nums[idx5] * 5));
// 由哪个idx生成新的丑数,哪个idx往后移,用于下一次生成丑数
// 三个if并列,用于去重
if(nums[i] == nums[idx2] * 2){
idx2++;
}
if(nums[i] == nums[idx3] * 3){
idx3++;
}
if(nums[i] == nums[idx5] * 5){
idx5++;
}
}
return nums[n-1];
}
};
2. set排序且去重
set可以排序,我们用set保存已经生成的丑数,取出最小的丑数分别乘以2、3、5就可以得到新的丑数,存入set后自动排序,第n轮循环删除最小的丑数,即是第n个丑数
class Solution {
public:
int nthUglyNumber(int n) {
set<double> s; // set 是有序的,且不重复
double answer = 1;
for (int i = 1; i < n; i++) {
s.insert(answer * 2);
s.insert(answer * 3);
s.insert(answer * 5);
answer = *s.begin();
s.erase(answer);
}
return answer;
}
};
边栏推荐
- PSM summary
- Gateway微服務路由使微服務靜態資源加載失敗
- Apache, IIS6 and ii7 independent IP hosts screen and intercept spider crawling (applicable to VPS virtual machine servers)
- 【522. 最长特殊序列 II】
- Feign远程调用fallback回调失败,无效果
- 目标检测|SSD原理与实现
- Mixed programming of C language and assembly language in stm32
- [today in history] June 23: Turing's birthday; The birth of the founder of the Internet; Reddit goes online
- You got 8K in the 3-year function test, but were overtaken by the new tester. In fact, you are pretending to work hard
- 面试:Bitmap像素内存分配在堆内存还是在native中
猜你喜欢

【活动早知道】LiveVideoStack近期活动一览
![[today in history] June 17: the creator of the term](/img/00/30ccc2f54415a6aca000c42e277dc3.png)
[today in history] June 17: the creator of the term "hypertext" was born; The birth of Novell's chief scientist; Discovery channel on

元宇宙标准论坛成立

2-5 basic configuration -win2003 add attack surface

What if win11 cannot use dynamic wallpaper? Solution of win11 without dynamic wallpaper

分布式事务解决方案Seata-Golang浅析

Summary of software testing tools in 2021 - fuzzy testing tools

More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
![[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper](/img/88/6cdd2b604522261e2a88020c5d6ae7.jpg)
[today in history] June 18: JD was born; The online store platform Etsy was established; Facebook releases Libra white paper
![[kotlin] basic introduction and understanding of its syntax in Android official documents](/img/44/ec59383ddfa2624a1616d13deda4a4.png)
[kotlin] basic introduction and understanding of its syntax in Android official documents
随机推荐
无心剑汉英双语诗004.《剑》
RichView TRVStyle
为什么大厂压力大,竞争大,还有这么多人热衷于大厂呢?
Apache——阿帕奇简介
面试:Bitmap像素内存分配在堆内存还是在native中
Opencv -- geometric space transformation (affine transformation and projection transformation)
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
分布式事务TCC浅析
The first in the industry! MOS sub evaluation model for subjective video quality experience that can run on mobile devices!
Artifact for converting pcap to JSON file: joy (installation)
Simple elk configuration to realize production level log collection and query practice
暴雨去哪儿?天气预报不准谁的锅?
Publicity of the third batch of shortlisted enterprises! Annual Top100 smart network supplier selection
[today in history] June 20: the father of MP3 was born; Fujitsu was established; Google acquires dropcam
Interview: how do lists duplicate objects according to their attributes?
Online text batch inversion by line tool
Heartless sword Chinese English bilingual poem 004 Sword
QEMU monitor usage
Thesis reading: General advantageous transformers
Simple file transfer protocol TFTP