当前位置:网站首页>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
边栏推荐
- [the 300th weekly match of leetcode]
- 首先看K一个难看的数字
- 传输层 拥塞控制-慢开始和拥塞避免 快重传 快恢复
- IP, subnet mask, gateway, default gateway
- [swoole series 2.1] run the swoole first
- Prophet模型的简介以及案例分析
- On time and parameter selection of asemi rectifier bridge db207
- Compilation Principle -- C language implementation of prediction table
- 徐翔妻子应莹回应“股评”:自己写的!
- Redis的五种数据结构
猜你喜欢
std::true_ Type and std:: false_ type
Introduction and case analysis of Prophet model
CSRF vulnerability analysis
J'aimerais dire quelques mots de plus sur ce problème de communication...
【.NET CORE】 请求长度过长报错解决方案
【LeetCode第 300 场周赛】
2019 Alibaba cluster dataset Usage Summary
Implementation of queue
Blue Bridge Cup real question: one question with clear code, master three codes
Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning 论文阅读
随机推荐
Docker安装Redis
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
High precision operation
Transport layer congestion control - slow start and congestion avoidance, fast retransmission, fast recovery
Running the service with systemctl in the container reports an error: failed to get D-Bus connection: operation not permitted (solution)
The latest financial report release + tmall 618 double top, Nike energy leads the next 50 years
华为0基金会——图片整理
ADB common commands
TCP packet sticking problem
Why does wechat use SQLite to save chat records?
Dichotomy (integer dichotomy, real dichotomy)
Brief description of SQL optimization problems
Virtual machine VirtualBox and vagrant installation
A method of sequentially loading Unity Resources
【LeetCode第 300 场周赛】
Self-supervised Heterogeneous Graph Neural Network with Co-contrastive Learning 论文阅读
declval(指导函数返回值范例)
Top command details
Ms-tct: INRIA & SBU proposed a multi-scale time transformer for motion detection. The effect is SOTA! Open source! (CVPR2022)...