当前位置:网站首页>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
边栏推荐
- Reprint: defect detection technology of industrial components based on deep learning
- 2022暑期项目实训(二)
- epoll()无论涉及wait队列分析
- TCP packet sticking problem
- Ms-tct: INRIA & SBU proposed a multi-scale time transformer for motion detection. The effect is SOTA! Open source! (CVPR2022)...
- Four processes of program operation
- Rb157-asemi rectifier bridge RB157
- Jielizhi obtains the customized background information corresponding to the specified dial [chapter]
- SQL优化问题的简述
- [Android] kotlin code writing standardization document
猜你喜欢

The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!

Take you through ancient Rome, the meta universe bus is coming # Invisible Cities

FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog

传输层 拥塞控制-慢开始和拥塞避免 快重传 快恢复

關於這次通信故障,我想多說幾句…

UDP protocol: simple because of good nature, it is inevitable to encounter "city can play"

Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design

模板于泛型编程之declval

declval(指导函数返回值范例)

Introduction and case analysis of Prophet model
随机推荐
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
44所高校入选!分布式智能计算项目名单公示
STM32+ESP8266+MQTT协议连接OneNet物联网平台
【.NET CORE】 请求长度过长报错解决方案
atcoder它A Mountaineer
F200 - UAV equipped with domestic open source flight control system based on Model Design
[swoole series 2.1] run the swoole first
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
Prophet模型的简介以及案例分析
Kill -9 system call used by PID to kill process
Maixll dock camera usage
Introduction to the usage of model view delegate principal-agent mechanism in QT
287. Find duplicates
Top command details
递归的方式
celery最佳实践
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
Echart simple component packaging
High precision operation
测试123