当前位置:网站首页>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
边栏推荐
猜你喜欢
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
Compilation principle - top-down analysis and recursive descent analysis construction (notes)
Maixll-Dock 摄像头使用
Why does wechat use SQLite to save chat records?
面向程序员的精品开源字体
Virtual machine VirtualBox and vagrant installation
Splay
Rb157-asemi rectifier bridge RB157
30 minutes to understand PCA principal component analysis
30 分钟看懂 PCA 主成分分析
随机推荐
Release of the sample chapter of "uncover the secrets of asp.net core 6 framework" [200 pages /5 chapters]
關於這次通信故障,我想多說幾句…
Comparative examples of C language pointers *p++, * (p++), * ++p, * (++p), (*p) + +, +(*p)
Jielizhi obtains the customized background information corresponding to the specified dial [chapter]
Jerry's watch deletes the existing dial file [chapter]
Principle and usage of extern
[the 300th weekly match of leetcode]
Distiller les connaissances du modèle interactif! L'Université de technologie de Chine & meituan propose Virt, qui a à la fois l'efficacité du modèle à deux tours et la performance du modèle interacti
Grafana 9.0 正式发布!堪称最强!
Implementation of queue
队列的实现
Kill -9 system call used by PID to kill process
Windows connects redis installed on Linux
2022/02/12
关于这次通信故障,我想多说几句…
使用block实现两个页面之间的传统价值观
echart简单组件封装
Ms-tct: INRIA & SBU proposed a multi-scale time transformer for motion detection. The effect is SOTA! Open source! (CVPR2022)...
Reprint: defect detection technology of industrial components based on deep learning
STM32+ESP8266+MQTT协议连接OneNet物联网平台