当前位置:网站首页>First, look at K, an ugly number
First, look at K, an ugly number
2022-07-06 18:25:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
Include only qualitative factors 2、3 and 5 The number of is called ugly (Ugly Number), such as :2,3,4,5,6,8,9,10,12,15, etc. , It's customary for us to 1 As the first ugly number . Write an efficient algorithm , Back to page n Ugly number .
import static java.lang.Math.min;
import static java.lang.System.out;
public class UglyNumber {
public static void main(String[] args) {
out.println(findKthUglyNumber(1500));
}
/**
* Search for the first K Ugly number
*
* @param k
* @return
*/
public static int findKthUglyNumber(int k) {
if (k < 0) {
return 1;// Return the first ugly number
}
int[] numbers = new int[k];
numbers[0] = 1;
int next = 1;
int ugly2Index = 0;
int ugly3Index = 0;
int ugly5Index = 0;
while (next < k) {
int uglyNum = min(numbers[ugly2Index] * 2,
min(numbers[ugly3Index] * 3, numbers[ugly5Index] * 5));
numbers[next] = uglyNum;
while (numbers[ugly2Index] * 2 <= numbers[next]) {
ugly2Index++;
}
while (numbers[ugly3Index] * 3 <= numbers[next]) {
ugly3Index++;
}
while (numbers[ugly5Index] * 5 <= numbers[next]) {
ugly5Index++;
}
next++;
}
return numbers[k - 1];// from 0 Start
}
}
Copyright notice : This article is an original blog article , Blog , Without consent , Shall not be reproduced .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/117395.html Link to the original text :https://javaforall.cn
边栏推荐
- Declval of template in generic programming
- Virtual machine VirtualBox and vagrant installation
- 關於這次通信故障,我想多說幾句…
- The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
- TCP packet sticking problem
- 2022/02/12
- 使用block实现两个页面之间的传统价值观
- Grafana 9.0 正式发布!堪称最强!
- Splay
- 最新财报发布+天猫618双榜第一,耐克蓄力领跑下个50年
猜你喜欢
287. Find duplicates
On time and parameter selection of asemi rectifier bridge db207
Grafana 9.0 is officially released! It's the strongest!
從交互模型中蒸餾知識!中科大&美團提出VIRT,兼具雙塔模型的效率和交互模型的性能,在文本匹配上實現性能和效率的平衡!...
Introduction to the usage of model view delegate principal-agent mechanism in QT
Jerry's updated equipment resource document [chapter]
C language exchanges two numbers through pointers
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
Docker安装Redis
Splay
随机推荐
UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
Codeforces Round #803 (Div. 2)
【.NET CORE】 请求长度过长报错解决方案
Excel usage record
2019 Alibaba cluster dataset Usage Summary
测试1234
队列的实现
Splay
Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning 论文阅读
徐翔妻子应莹回应“股评”:自己写的!
Five data structures of redis
Insert dial file of Jerry's watch [chapter]
图片缩放中心
C语言自动预订飞机票问题
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
使用cpolar建立一个商业网站(1)
模板于泛型编程之declval
node の SQLite
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
Rb157-asemi rectifier bridge RB157