当前位置:网站首页>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;
}
};
边栏推荐
- MySQL gets the current date of the year
- Excel knowledge and skills summary
- 27 years, Microsoft IE is over!
- Ppt production tips
- In the era of video explosion, who is supporting the high-speed operation of video ecological network?
- Secouer le son et se battre ~ prêter attention au blogueur
- Learning about DC-DC step-down chip of sy8120i (12V reduced to 3.3V)
- 请问下,mysqlcdc设置多并行度的话,增量数据是不是会重复?
- The coming wave of Web3
- Multithreading and high concurrency III: AQS underlying source code analysis and implementation classes
猜你喜欢

Secouer le son et se battre ~ prêter attention au blogueur

Problems with cat and dog queues

抖音实战~取关博主

一文详解|增长那些事儿

Web3来临时的风口浪尖

June 27, 2022: give a 01 string with a length of N. now please find two intervals so that the number of 1 and the number of 0 in the two intervals are equal. The two intervals can intersect, but not c

政策利好,20多省市开启元宇宙发展规划

Recommended by Alibaba P8, Fiddler packet capturing tool (I)

first. Net core MVC project
![[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]](/img/73/1e4c605991189acc674d85618cf0ef.png)
[Matlab bp regression prediction] GA Optimized BP regression prediction (including comparison before optimization) [including source code 1901]
随机推荐
JS逆向之巨量星图sign签名
Why are cloud vendors targeting this KPI?
Mise en place d'un cadre d'essai d'automatisation de l'interface utilisateur - - rédaction d'une application d'automatisation
Aspnetcoreratelimit rate limit interface access limit current limit control
Secouer le son et se battre ~ prêter attention au blogueur
RT-Thread 双向链表(学习笔记)
[matlab traffic light identification] traffic light identification [including GUI source code 1908]
Are test / development programmers really young? The world is fair. We all speak by strength
阿里P8倾情推荐,Fiddler抓包工具实战篇(一)
Establishment of SSH Framework (Part 2)
TFTLCD display experiment of mini plate based on punctual atom stm32
The coming wave of Web3
Uncover the mystery of SSL and learn how to protect data with SSL
Multithreading and high concurrency III: AQS underlying source code analysis and implementation classes
Pinda general permission system (day 5~day 6)
在线直播源码,JS动态效果之,侧边栏滚动固定效果
Multithreading and high concurrency II: detailed introduction to volatile and CAS
Why is the frame rate calculated by opencv wrong?
leetcode:714. 买卖股票的最佳时机含手续费【dp双状态】
Games104 operation 2-colorgrading