当前位置:网站首页>剑指 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;
}
};
边栏推荐
- 树莓派-环境设置和交叉编译
- 数字化时代,企业须做好用户信息安全
- [today in history] June 19: iPhone 3GS launched; Pascal was born; Anti terrorist elite begins testing
- 为什么大厂压力大,竞争大,还有这么多人热衷于大厂呢?
- Basic flask: template rendering + template filtering + control statement
- Simple elk configuration to realize production level log collection and query practice
- Built in functions for MySQL database operations
- 无心剑汉英双语诗004.《剑》
- 第一次使用gcc和makefile编写c程序
- Le routage des microservices de la passerelle a échoué au chargement des ressources statiques des microservices
猜你喜欢

字节跳动面试官:一张图片占据的内存大小是如何计算

如何编写简洁代码?(上)

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

简单ELK配置实现生产级别的日志采集和查询实践

2021年软件测试工具总结——模糊测试工具

How to run unity webgl after packaging (Firefox configuration)

Domain Name System

Writing C program with GCC and makefile for the first time

CMU puts forward a new NLP paradigm - reconstructing pre training, and achieving 134 high scores in college entrance examination English

Reprinted article: the digital economy generates strong demand for computing power Intel releases a number of innovative technologies to tap the potential of computing power
随机推荐
抓包整理外篇fiddler————了解工具栏[一]
目标检测|SSD原理与实现
Gateway微服務路由使微服務靜態資源加載失敗
Mysql database operation - stored procedure, view, transaction, index, database backup
Publicity of the third batch of shortlisted enterprises! Annual Top100 smart network supplier selection
您的物联网安全性是否足够强大?
导致系统性能失败的十个原因
Usage differences between isempty and isblank
数字化时代,企业须做好用户信息安全
Severe Tire Damage:世界上第一个在互联网上直播的摇滚乐队
[today in history] June 6: World IPv6 launch anniversary; Tetris release; Little red book established
A16z:元宇宙解锁游戏基础设施中的新机遇
[today in history] June 16: Oracle Bone Inscriptions was established; Microsoft MSX was born; The inventor of fast Fourier transform was born
Raspberry pie - environment settings and cross compilation
The horizontal scrolling recycleview displays five and a half in one screen, which is lower than the five average distributions
How fiddle uses agents
CMU提出NLP新范式—重构预训练,高考英语交出134高分
JDBC and MySQL databases
【iptables&icmp】iptables默认策略中关于icmp协议的说明
[today in history] June 11: the co inventor of Monte Carlo method was born; Google launched Google Earth; Google acquires waze