当前位置:网站首页>【快排】215. 数组中的第K个最大元素
【快排】215. 数组中的第K个最大元素
2022-04-23 13:07:00 【不爱研究的研究僧】
题目:
给定整数数组 nums 和整数 k,请返回数组中第 k 个最大的元素。
请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

题解:
快排每轮partition就可以将选中的pivot放到其最终有序位置,当某轮pivot被放到第k大元素位置(从小到大有序序列的下标n-k处)时,确定该pivot为结果
pivot的选择利用随机数可以防止最差时间复杂度。
nextInt()用法:
会随机生成一个整数,这个整数的范围就是int类型的范围-2^31 ~ 2^31-1,但是如果在nextInt()括号中加入一个整数a那么,这个随机生成的随机数范围就变成[0,a)。
class Solution {
public int findKthLargest(int[] nums, int k) {
//第k大元素,即排序后下标为n-k的元素,使用partition方法确定下标n-k元素
int target = nums.length - k;
int from = 0, to = nums.length - 1;
while (true) {
int idx = partition(nums, from, to); //每次partition返回pivot插入的位置
if (idx == target) { //如果插入的位置是目标位置,找到第k大元素
return nums[target];
} else if (idx > target) { //如果插入的位置大于目标位置,目标位置搜索范围应该在idx左边
to = idx - 1;
} else { //如果插入的位置小于目标位置,目标位置搜索范围应该在idx右边
from = idx + 1;
}
}
}
Random rand = new Random();
//返回单次partition确定pivot元素固定到的位置
private int partition(int[] nums, int from, int to) {
int pivotIndex = rand.nextInt(to - from + 1) + from;
int pivot = nums[from];
//随机pivot交换到头部
int temp = nums[from];
nums[from] = pivot;
nums[pivotIndex] = temp;
int left = from, right = to;
while (left < right) {
while (left < right && nums[right] >= pivot) { //从右往左找到第一个<pivot的
right--;
}
if (left < right) {
nums[left] = nums[right];
}
while (left < right && nums[left] <= pivot) { //从左往右找到第一个>pivot的
left++;
}
if (left < right) {
nums[right] = nums[left];
}
}
nums[left] = pivot; //最后把pivot放到left的位置
return left;
}
}
参考:力扣
版权声明
本文为[不爱研究的研究僧]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43955488/article/details/124360180
边栏推荐
- How to convert opencv pictures to bytes
- 拥抱机器视觉新蓝海,冀为好望开启数字经济发展新“冀”遇
- V-model binding value in El select, data echo only displays value, not label
- Temperature and humidity monitoring + timing alarm system based on 51 single chip microcomputer (C51 source code)
- nodeJs + websocket 循环小案例
- 100 lectures on practical application cases of Excel (VIII) - report connection function of Excel
- 22. 括号生成
- uniapp image 引入本地图片不显示
- Golang implements MD5, sha256 and bcrypt encryption
- Mui close other pages and keep only the first page
猜你喜欢

Important knowledge of network layer (interview, reexamination, term end)

MySQL -- 16. Data structure of index

Mysql8 installation

Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy

Use compressorjs to compress pictures, optimize functions, and compress pictures in all formats

22. Bracket generation

mysql8安装

Remote access to raspberry pie at home (Part 1)

PC starts multiple wechat at one time

Melt reshape decast long data short data length conversion data cleaning row column conversion
随机推荐
Utils of various date conversion
Temperature and humidity monitoring + timing alarm system based on 51 single chip microcomputer (C51 source code)
[51 single chip microcomputer traffic light simulation]
Learning notes of AMBA protocol
Important knowledge of transport layer (interview, retest, final)
7_Addmodule和基因加和法add 得到的细胞类型打分在空间上空转对比
The accuracy and speed are perfectly balanced, and the latest image segmentation SOTA model is released!!!
AUTOSAR from introduction to mastery 100 lectures (83) - bootloader self refresh
pyqt5 将opencv图片存入内置SQLlite数据库,并查询
The use of dcast and melt in R language is simple and easy to understand
100 GIS practical application cases (34) - splicing 2020globeland30
初鉴canvas,展示个小小的小案例
100 GIS practical application cases (51) - a method for calculating the hourly spatial average of NC files according to the specified range in ArcGIS
将新增和编辑的数据同步更新到列表
FFmpeg常用命令
mui + hbuilder + h5api模拟弹出支付样式
The quill editor image zooms, multiple rich text boxes are used on one page, and the quill editor upload image address is the server address
Async void provoque l'écrasement du programme
Navicat远程连接数据库 出现 1130- Host xxx is not allowed to connect to this MySQL server错误
Go language mapping operation