当前位置:网站首页>[sword finger offer] 49 Ugly number
[sword finger offer] 49 Ugly number
2022-06-28 11:00:00 【LuZhouShiLi】
The finger of the sword Offer 49. Ugly number
subject
We will include only qualitative factors 2、3 and 5 The number of is called ugly (Ugly Number). Seek the order from small to large n Ugly number .
Ideas
Ugly numbers contain only factors 2,3,5, So there is “ Ugly number = Some clown number x Some factor ”
Ugly number Xn+1 It can only be one of the following three cases :
* Xn+1 = Xa * 2
* Xn+1 = Xb * 3
* Xn+1 = Xc * 5
Ugly number recurrence formula : The next ugly number is the minimum of these three cases :
Xn+1 = min(Xa x 2,Xb x 3, Xc x 5)
Code
class Solution {
public:
int nthUglyNumber(int n) {
int a = 0,b = 0,c = 0;
int dp[n];
dp[0] = 1;
for(int i = 1; i < n; i++)
{
int n2 = dp[a] * 2;
int n3 = dp[b] * 3;
int n5 = dp[c] * 5;
dp[i] = min(min(n2,n3),n5);
if(dp[i] == n2) a++;
if(dp[i] == n3) b++;
if(dp[i] == n5) c++;
}
return dp[n - 1];
}
};
边栏推荐
- Understanding of FTP protocol
- 选择哪种编程语言,会吸引优秀的人才?
- 阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
- Information hidden in the trend chart of Hong Kong London gold market
- Katalon全局变量在TestObject引用
- Katalon当中的debug调试
- Solve the problem of reading package listsdonebuilding dependency treereading state informationdone
- 一种跳板机的实现思路
- Katalon global variable is referenced in testobject
- Who knows if it is safe to open an account with CSC securities
猜你喜欢

How to use output in katalon

Remote connection of raspberry pie in VNC viewer mode without display

建立自己的网站(11)

Threads and thread pools

Wireless communication module fixed-point transmission - point to multipoint specific transmission application

Katalon当中的output使用方法

数据库系列:有什么办法对数据库的业务表进行无缝升级

动态库(共享库)的制作和使用

远程登录sshd服务

Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
随机推荐
Threads and thread pools
Minimum stack < difficulty coefficient >
【功能建议】多个工作空间启动时选择某个空间
【SemiDrive源码分析】【X9芯片启动流程】32 - DisPlay模块分析 - RTOS侧
Knowing the details of redis RDB, you can step on many holes less
[Agora] get an Agora_ Example usage of refptr object
Convert the file URL in the browser to a file stream
乌国家安全与国防委员会秘书:将对俄境内目标进行精确打击
JS基础10
[monkey] Introduction to monkey test
Does flink1.15 support MySQL views? I configured the view name at the table name to save, but the table could not be found. Think
阿里三面:LEFT JOIN关联表中用ON还是WHERE跟条件有什么区别
JS基础2
Wireless communication module fixed-point transmission - point to multipoint specific transmission application
Metersphere uses JS to refresh the current page
MySQL(二)
第2章 还记得点、线、面吗(二)
Katalon全局变量在TestObject引用
flink1.15,支持mysql视图吗?我这边在table-name处配置视图名保存,找不到表。想
How to use output in katalon