当前位置:网站首页>[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];
}
};
边栏推荐
猜你喜欢
随机推荐
东方财富手机股票开户哪个券商更安全更方便?
GCC简介
MySQL (III)
Transactions proof in appliedzkp zkevm (10)
毕业季 新的开始
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
获取系统当前日期
MySQL general binary installation method
Katalon当中的使用循环for、while和if...else、break、continue
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!
Xshell和Xftp使用教程
利用soapUI获取freemarker的ftl文件模板
Summary of spatial temporal time series prediction modeling methods
soapui的菜鸟教程
个人买场内基金选择什么证券公司开户好,更安全
Ble Bluetooth module nrf518/nrf281/nrf528/nrf284 chip scheme comparison
使用API快捷创建ECS
合约量化系统开发(搭建讲解)丨合约量化系统开发(源码解析及现成案例)
Set up your own website (11)
使用logrotate对宝塔的网站日志进行自动切割









![[practice] 1364- implement a perfect waterfall flow component on the mobile terminal (with source code)](/img/e8/21d8d81a3d7b544687d6adc06ad4b1.png)