当前位置:网站首页>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;
}
};
边栏推荐
- 测试开发必备技能:安全测试漏洞靶场实战
- [small program practice series] e-commerce platform source code and function implementation
- The SQL of filincdc always reports this error when there are multiple tables. How can I solve it
- 03 summary of various additions, updates and deletions of mongodb documents
- 求一个能判断表中数据,只填充不覆盖的sql
- [MySQL] multi table connection query
- 2022年中国音频市场年度综合分析
- Reading notes of top performance version 2 (II) -- CPU monitoring
- Tiktok actual battle ~ take off the blogger
- GO语言-select语句
猜你喜欢

控制器的功能和工作原理
![[small program practice series] e-commerce platform source code and function implementation](/img/a4/581d73c2ff5c190edc3d8438fa2122.png)
[small program practice series] e-commerce platform source code and function implementation

测试开发必备技能:安全测试漏洞靶场实战

Mise en place d'un cadre d'essai d'automatisation de l'interface utilisateur - - rédaction d'une application d'automatisation

Pinda general permission system (day 5~day 6)

Matlab exercises -- exercises related to symbolic operation

Reading notes of top performance version 2 (II) -- CPU monitoring

公司领导说,个人代码超10个Bug就开除,是什么体验?

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

From meeting a big guy to becoming a big guy, shengteng AI developer creation day brings infinite possibilities to developers
随机推荐
The second round of free public classes of the red team is coming ~ 8:00 tomorrow night!
Google Earth engine (GEE) - global flood database V1 (2000-2018)
Multithreading and high concurrency IV: varhandle, strong weak virtual reference and ThreadLocal
To quickly download JDK, in addition to the official Oracle download, is there a download address for the latest version available in China
Are test / development programmers really young? The world is fair. We all speak by strength
Multithreading and high concurrency six: source code analysis of thread pool
公司领导说,个人代码超10个Bug就开除,是什么体验?
求一个能判断表中数据,只填充不覆盖的sql
Pager when importing text files from MySQL
AspNetCoreRateLimit 速率限制 接口访问限制 限流控制
有大佬出现过mysql cdc用 datastream时,出现重复binlog消息的情况吗
Lazy loading and preloading of pictures
UI automation test framework construction - write an app automation
CDC全量抽取mysql数据时,怎么才能加快ChunkSplitter呢?
Web3来临时的风口浪尖
如何遍历collections.OrderedDict,服了又忘记items
Ppt production tips
[matlab traffic light identification] traffic light identification [including GUI source code 1908]
Aspnetcoreratelimit rate limit interface access limit current limit control
Live online source code, JS dynamic effect, sidebar scrolling fixed effect