当前位置:网站首页>【快排】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
边栏推荐
- 解决虚拟机中Oracle每次要设置ip的问题
- Async void provoque l'écrasement du programme
- 8086 of x86 architecture
- Three channel ultrasonic ranging system based on 51 single chip microcomputer (timer ranging)
- filter()遍历Array异常友好
- 100 GIS practical application cases (34) - splicing 2020globeland30
- uniapp image 引入本地图片不显示
- Van uploader upload picture implementation process, using native input to upload pictures
- These vscode extensions are my favorite
- Community version Alibaba MQ ordinary message sending subscription demo
猜你喜欢
Mui + hbuilder + h5api simulate pop-up payment style
HQL find the maximum value in a range
5 free audio material websites, recommended collection
PC starts multiple wechat at one time
mysql8安装
Design of body fat detection system based on 51 single chip microcomputer (51 + OLED + hx711 + US100)
AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
Kernel error: no rule to make target 'Debian / canonical certs pem‘, needed by ‘certs/x509_ certificate_ list‘
拥抱机器视觉新蓝海,冀为好望开启数字经济发展新“冀”遇
将新增和编辑的数据同步更新到列表
随机推荐
Record Alibaba cloud server mining program processing
The filter() traverses the array, which is extremely friendly
mysql支持ip访问
nodejs + mysql 实现简单注册功能(小demo)
Start mqbroker CMD failure resolution
Proteus 8.10 installation problem (personal test is stable and does not flash back!)
Learning notes of AMBA protocol
MySQL —— 16、索引的数据结构
【微信小程序】flex布局使用记录
AUTOSAR from introduction to mastery lecture 100 (84) - Summary of UDS time parameters
AUTOSAR from introduction to mastery 100 lectures (86) - 2F of UDS service foundation
Design of STM32 multi-channel temperature measurement wireless transmission alarm system (industrial timing temperature measurement / engine room temperature timing detection, etc.)
这几种 VSCode 扩展是我最喜欢的
(1) Openjuterpyrab comparison scheme
Golang implements a five insurance and one gold calculator with web interface
100 lectures on practical application cases of Excel (VIII) - report connection function of Excel
Async void caused the program to crash
V-model binding value in El select, data echo only displays value, not label
Install nngraph
Translation of attention in natural language processing