题目要求
题目链接
《查找第K大/小元素算法》
这题不使用线性复杂度算法是过不去的(除非你用C++的[内置函数]or[快读+sort()]) Java应该是不可能过去的,你用C++可能卡在T上,Java还没到T呢,M直接卡爆了,顶多60分水平吧。
下面是Java版本,线性复杂度,尽可能优化,60分:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 线性复杂度算法
* 该算法的C++版本可AC,但Java版本就会MLE/TLE
* 该算法C++版本跑的比nth_element()跑得快
*/
public class Main {
private static int[] nums;
private static int k, num;
private static void swap(int left, int right) {
int temp = nums[left];
nums[left] = nums[right];
nums[right] = temp;
}
private static void sort(int left, int right) {
int left_mid=left, right_mid=right, mid= nums[(left+right)/2];
while (left_mid mid) {
right_mid--;
}
while (nums[left_mid]
关注
打赏
热门博文
- 【Linux】Ubuntu20.04安装和卸载MySQL8
- 【Linux】Ubuntu 20.04 报错 curl: (23) Failure writing output to destination 的解决方法
- 【Java】JUnit 4.13.2 警告 ‘assertEquals(double, double)‘ is deprecated 的解决方法
- 【JavaScript】处理 @parcel/transformer-js: Browser scripts cannot have imports or exports.
- 【Node.js】Windows环境安装配置NVM和Node.js
- 【Python】处理TypeError: Plain typing.NoReturn is not valid as type argument
- 【Python】Matplotlib可视化50例
- 【C语言】C语言修改MySQL数据库
- 【Java】从默认包导入类和对象报错的解决方法
- 【Java】panel.getGraphics()报错空指针异常的解决方法