当前位置:网站首页>首先看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
边栏推荐
- 小程序在产业互联网中的作用
- TOP命令详解
- 阿里云国际版ECS云服务器无法登录宝塔面板控制台
- Redis的五种数据结构
- 递归的方式
- 编译原理——自上而下分析与递归下降分析构造(笔记)
- Take you through ancient Rome, the meta universe bus is coming # Invisible Cities
- On time and parameter selection of asemi rectifier bridge db207
- The difference between parallelism and concurrency
- Transport layer congestion control - slow start and congestion avoidance, fast retransmission, fast recovery
猜你喜欢

The easycvr authorization expiration page cannot be logged in. How to solve it?

队列的实现

How to solve the error "press any to exit" when deploying multiple easycvr on one server?

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

STM32按键状态机2——状态简化与增加长按功能

C语言通过指针交换两个数

Prophet模型的简介以及案例分析

Interesting - questions about undefined

node の SQLite

It doesn't make sense without a distributed gateway
随机推荐
Codeforces Round #803 (Div. 2)
Rb157-asemi rectifier bridge RB157
TCP packet sticking problem
Is it meaningful for 8-bit MCU to run RTOS?
Will openeuler last long
SAP Fiori 应用索引大全工具和 SAP Fiori Tools 的使用介绍
趣-关于undefined的问题
Selected technical experts from China Mobile, ant, SF, and Xingsheng will show you the guarantee of architecture stability
Video fusion cloud platform easycvr adds multi-level grouping, which can flexibly manage access devices
Why does wechat use SQLite to save chat records?
Codeforces Round #803 (Div. 2)
微信为什么使用 SQLite 保存聊天记录?
Top command details
Principle and usage of extern
Smart street lamp based on stm32+ Huawei cloud IOT 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
Getting started with pytest ----- test case pre post, firmware
F200——搭载基于模型设计的国产开源飞控系统无人机
STM32 key state machine 2 - state simplification and long press function addition
【.NET CORE】 请求长度过长报错解决方案