当前位置:网站首页>Sword finger offer 49 Ugly number (three finger needling technique)
Sword finger offer 49 Ugly number (three finger needling technique)
2022-06-28 04:40:00 【BugMaker-shen】
One 、263. Ugly number

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;
}
};
Two 、264. Ugly number II

1. Three finger needling

class Solution {
public:
int nthUglyNumber(int n) {
int idx2 = 0;
int idx3 = 0;
int idx5 = 0;
vector<int> nums(n, 0);
nums[0] = 1; // The first ugly number is 1
for(int i = 1; i < n; i++){
// Generate new ugly numbers from the ugly numbers in the array , Store the smallest ugly number in the array
nums[i] = min(nums[idx2] * 2, min(nums[idx3] * 3, nums[idx5] * 5));
// Which is from idx Generate new ugly numbers , Which one? idx Move back , Used for the next generation of ugly numbers
// Three if Juxtaposition , Used to remove heavy
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 Sorting and de duplication
set Can sort , We use it set Save the generated ugly numbers , Take the smallest ugly number and multiply it by 2、3、5 You can get a new ugly number , Deposit in set Auto sort after , The first n Round robin deletes the smallest ugly number , This is the first n Ugly number
class Solution {
public:
int nthUglyNumber(int n) {
set<double> s; // set Is ordered , And not repeated
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;
}
};
边栏推荐
- How to clean the nozzle of Epson l3153 printer
- Live online source code, JS dynamic effect, sidebar scrolling fixed effect
- 抖音實戰~關注博主
- Why are cloud vendors targeting this KPI?
- Why is the frame rate calculated by opencv wrong?
- flinkcdc采集oracle,oracle数据库是CDB的
- Database garbled
- Secouer le son et se battre ~ prêter attention au blogueur
- The company leader said that if the personal code exceeds 10 bugs, he will be dismissed. What is the experience?
- Pinda general permission system (day 5~day 6)
猜你喜欢

Google Earth engine (GEE) - global flood database V1 (2000-2018)

成长一夏 挑战赛来袭 | 学习、创作两大赛道,开启导师报名啦!

Pinda general permission system (day 5~day 6)

mysql修改密码报错需要怎么做

Audio and video technology development weekly

Why are cloud vendors targeting this KPI?

2022年中國音頻市場年度綜合分析

Reading notes of top performance version 2 (II) -- Performance observation tool

浅析搭建视频监控汇聚平台的必要性及场景应用

first. Net core MVC project
随机推荐
Matlab exercises -- basic data processing
Pinda general permission system (day 5~day 6)
Is it better for a novice to open a securities account? Is it safe to open a stock account
TFTLCD display experiment of mini plate based on punctual atom stm32
多线程实现 重写run(),怎么注入使用mapper文件操作数据库
2022年中国音频市场年度综合分析
Moonbeam integrates coin98, giving users more choices on the multi chain road
[proteus simulation] timer 1 external counting interrupt
2022-06-27:给出一个长度为n的01串,现在请你找到两个区间, 使得这两个区间中,1的个数相等,0的个数也相等, 这两个区间可以相交,但是不可以完全重叠,即两个区间的左右端点不可以完全一样。
Is it true that qiniu business school gives away securities accounts? Is it safe to open an account
11_ Deliberate practice and elaboration
How can we speed up the chunksplitter when CDC extracts MySQL data in full?
【Linux】【Mysql】ERROR 1698 (28000): Access denied for user ‘root‘@‘localhost‘
成长一夏 挑战赛来袭 | 学习、创作两大赛道,开启导师报名啦!
有人用cdc同步到mysql发生过死锁吗?
leetcode:714. The best time to buy and sell stocks includes handling fee [DP dual status]
Digital promising, easy to reach, Huawei accelerates the layout of the commercial market with "five pole" star products
求一个能判断表中数据,只填充不覆盖的sql
在线直播源码,JS动态效果之,侧边栏滚动固定效果
E-week finance Q1 mobile banking has 650million active users; Layout of financial subsidiaries in emerging fields