当前位置:网站首页>[sword finger offer] interview question 49: ugly number
[sword finger offer] interview question 49: ugly number
2022-07-27 15:52:00 【Jocelin47】

Method 1 : Order to judge
By traversing from 1 Each number at the beginning gets the result , Algorithm efficiency is not very high
class Solution {
public:
int nthUglyNumber(int n) {
if( n <= 0 )
return 0;
int index_number = 0;
int Ugly_number = 0;
while ( Ugly_number < n)
{
++index_number;
if( IsUgly(index_number) )
Ugly_number++;
}
return index_number;
}
bool IsUgly(int number)
{
while( number % 2 == 0 )
number /= 2;
while( number % 3 == 0 )
number /= 3;
while( number % 5 == 0 )
number /= 5;
return ( number == 1 ) ? true : false;
}
};
Method 2 : Space for time
The efficiency of the previous algorithm is slow because we need to calculate it whether it is ugly or not .
So you can try to find a way to calculate only ugly numbers , Don't spend time on integers that are not ugly numbers .
class Solution {
public:
int nthUglyNumber(int n) {
vector<int> dp(n,0);
dp[0] = 1;
int p2 = 0,p3 = 0,p5 = 0;
for( int i = 1; i < n; i++ )
{
dp[i] = min( min( dp[p2]*2, dp[p3]*3), dp[p5]*5 );
if(dp[i] == dp[p2] * 2)
p2++;
if(dp[i] == dp[p3] * 3)
p3++;
if(dp[i] == dp[p5] * 5)
p5++;
}
return dp[n-1];
}
};
边栏推荐
- 网络设备硬核技术内幕 路由器篇 小结(下)
- Implementation of spark lazy list files
- 初识MySQL数据库
- leetcode-1:两数之和
- 传美国政府将向部分美企发放对华为销售许可证!
- [tensorboard] oserror: [errno 22] invalid argument processing
- 实体类(VO,DO,DTO)的划分
- [regular expression] single character matching
- [sword finger offer] interview question 41: median in data flow - large and small heap implementation
- MySQL表数据的增删查改
猜你喜欢

Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers

Complexity analysis

synchronized和ReentrantLock的区别

【剑指offer】面试题46:把数字翻译成字符串——动态规划
![[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN](/img/01/bbf81cccb47b6351d7265ee4a77c55.png)
[sword finger offer] interview question 42: the maximum sum of continuous subarrays -- with 0x80000000 and int_ MIN

Inter thread wait and wake-up mechanism, singleton mode, blocking queue, timer

网络层的IP协议

Spark troubleshooting finishing

IP protocol of network layer

【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
随机推荐
C language: function stack frame
$router.back(-1)
Using Prometheus to monitor spark tasks
go语言慢速入门——包
Read the wheelevent in one article
实体类(VO,DO,DTO)的划分
It is said that the US government will issue sales licenses to Huawei to some US enterprises!
Static关键字的三种用法
[system programming] process, thread problem summary
【剑指offer】面试题49:丑数
Half find
C: What is the return value (revolution) in a function
Alibaba's latest summary 2022 big factory interview real questions + comprehensive coverage of core knowledge points + detailed answers
C语言:自定义类型
【剑指offer】面试题56-Ⅰ:数组中数字出现的次数Ⅰ
go语言慢速入门——go运算符
【剑指offer】面试题50:第一个只出现一次的字符——哈希表查找
Voice live broadcast system -- a necessary means to improve the security of cloud storage
synchronized和ReentrantLock的区别
聊聊ThreadLocal