当前位置:网站首页>首先看K一个难看的数字
首先看K一个难看的数字
2022-07-06 10:19:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
把仅仅包括质因子2、3和5的数称作丑数(Ugly Number),比如:2,3,4,5,6,8,9,10,12,15,等,习惯上我们把1当做是第一个丑数。 写一个高效算法,返回第n个丑数。
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));
}
/**
* 寻找第K个丑数
*
* @param k
* @return
*/
public static int findKthUglyNumber(int k) {
if (k < 0) {
return 1;// 把第一个丑数返回
}
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];// 从0開始
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117395.html原文链接:https://javaforall.cn
边栏推荐
- 编译原理——自上而下分析与递归下降分析构造(笔记)
- 二分(整数二分、实数二分)
- Jerry's watch deletes the existing dial file [chapter]
- STM32 key state machine 2 - state simplification and long press function addition
- F200——搭载基于模型设计的国产开源飞控系统无人机
- Excellent open source fonts for programmers
- Why does wechat use SQLite to save chat records?
- Jerry's updated equipment resource document [chapter]
- Declval of template in generic programming
- Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
猜你喜欢
C language exchanges two numbers through pointers
Recursive way
Virtual machine VirtualBox and vagrant installation
Distinguish between basic disk and dynamic disk RAID disk redundant array
30 minutes to understand PCA principal component analysis
Kivy tutorial: support Chinese in Kivy to build cross platform applications (tutorial includes source code)
Heavy! Ant open source trusted privacy computing framework "argot", flexible assembly of mainstream technologies, developer friendly layered design
Distill knowledge from the interaction model! China University of science and Technology & meituan proposed virt, which combines the efficiency of the two tower model and the performance of the intera
The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
面向程序员的精品开源字体
随机推荐
Codeforces Round #803 (Div. 2)
Grafana 9.0 正式发布!堪称最强!
李書福為何要親自掛帥造手機?
【Swoole系列2.1】先把Swoole跑起来
D binding function
微信小程序中给event对象传递数据
ASEMI整流桥DB207的导通时间与参数选择
Getting started with pytest ----- test case pre post, firmware
Is it meaningful for 8-bit MCU to run RTOS?
Compilation principle - top-down analysis and recursive descent analysis construction (notes)
2019阿里集群数据集使用总结
面试突击62:group by 有哪些注意事项?
Windows connects redis installed on Linux
Olivetin can safely run shell commands on Web pages (Part 1)
C语言指针*p++、*(p++)、*++p、*(++p)、(*p)++、++(*p)对比实例
Scratch epidemic isolation and nucleic acid detection Analog Electronics Society graphical programming scratch grade examination level 3 true questions and answers analysis June 2022
Declval of template in generic programming
Insert dial file of Jerry's watch [chapter]
2022暑期项目实训(三)
2022暑期项目实训(一)