当前位置:网站首页>首先看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
边栏推荐
- Ms-tct: INRIA & SBU proposed a multi-scale time transformer for motion detection. The effect is SOTA! Open source! (CVPR2022)...
- The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
- 2022暑期项目实训(三)
- Jerry is the custom background specified by the currently used dial enable [chapter]
- UDP协议:因性善而简单,难免碰到“城会玩”
- 2022暑期项目实训(二)
- The integrated real-time HTAP database stonedb, how to replace MySQL and achieve nearly a hundredfold performance improvement
- MarkDown语法——更好地写博客
- It doesn't make sense without a distributed gateway
- TOP命令详解
猜你喜欢
从交互模型中蒸馏知识!中科大&美团提出VIRT,兼具双塔模型的效率和交互模型的性能,在文本匹配上实现性能和效率的平衡!...
C语言通过指针交换两个数
Jerry's updated equipment resource document [chapter]
SAP Fiori 应用索引大全工具和 SAP Fiori Tools 的使用介绍
C language exchanges two numbers through pointers
FMT开源自驾仪 | FMT中间件:一种高实时的分布式日志模块Mlog
[Android] kotlin code writing standardization document
Pourquoi Li shufu a - t - il construit son téléphone portable?
QT中Model-View-Delegate委托代理机制用法介绍
On time and parameter selection of asemi rectifier bridge db207
随机推荐
Prophet模型的简介以及案例分析
Jerry's watch reading setting status [chapter]
[swoole series 2.1] run the swoole first
CRMEB 商城系统如何助力营销?
Getting started with pytest ----- test case pre post, firmware
[Android] kotlin code writing standardization document
简单易用的PDF转SVG程序
【.NET CORE】 请求长度过长报错解决方案
Jerry's setting currently uses the dial. Switch the dial through this function [chapter]
李書福為何要親自掛帥造手機?
第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
C language exchanges two numbers through pointers
win10系统下插入U盘有声音提示却不显示盘符
Compilation Principle -- C language implementation of prediction table
The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!
Ms-tct: INRIA & SBU proposed a multi-scale time transformer for motion detection. The effect is SOTA! Open source! (CVPR2022)...
【Android】Kotlin代码编写规范化文档
Jielizhi obtains the customized background information corresponding to the specified dial [chapter]
阿里云国际版ECS云服务器无法登录宝塔面板控制台
小程序在产业互联网中的作用