当前位置:网站首页>[quick platoon] 215 The kth largest element in the array
[quick platoon] 215 The kth largest element in the array
2022-04-23 13:11:00 【A research monk who doesn't like research】
subject :
Given an array of integers nums
And integer k
, Please return the... In the array k
The biggest element .
Please note that , What you need to look for is the number after array sorting k
The biggest element , Not the first. k
A different element .
Answer key :
Line up every round partition You can put the selected pivot Put it in its final ordered position , When a round pivot Put in the second place k Large element location ( Subscript of ordered sequence from small to large n-k It's about ) when , Determine the pivot As a result
pivot The choice of using random numbers can prevent the worst time complexity .
nextInt() usage :
Will randomly generate an integer , The range of this integer is int The range of types -2^31 ~ 2^31-1, But if nextInt() Add an integer in parentheses a that , The range of randomly generated random numbers becomes [0,a).
class Solution {
public int findKthLargest(int[] nums, int k) {
// The first k Big element , That is, the subscript after sorting is n-k The elements of , Use partition Method to determine the subscript n-k Elements
int target = nums.length - k;
int from = 0, to = nums.length - 1;
while (true) {
int idx = partition(nums, from, to); // Every time partition return pivot Insertion position
if (idx == target) { // If the insertion position is the target position , Find No k Big element
return nums[target];
} else if (idx > target) { // If the insertion position is greater than the target position , The target location search range should be idx On the left
to = idx - 1;
} else { // If the insertion position is less than the target position , The target location search range should be idx On the right
from = idx + 1;
}
}
}
Random rand = new Random();
// Return single partition determine pivot Where the element is fixed
private int partition(int[] nums, int from, int to) {
int pivotIndex = rand.nextInt(to - from + 1) + from;
int pivot = nums[from];
// Random pivot Swap to the head
int temp = nums[from];
nums[from] = pivot;
nums[pivotIndex] = temp;
int left = from, right = to;
while (left < right) {
while (left < right && nums[right] >= pivot) { // Find the first one from right to left <pivot Of
right--;
}
if (left < right) {
nums[left] = nums[right];
}
while (left < right && nums[left] <= pivot) { // Find the first one from left to right >pivot Of
left++;
}
if (left < right) {
nums[right] = nums[left];
}
}
nums[left] = pivot; // Finally, put pivot Put it in left The location of
return left;
}
}
Reference resources : Power button
版权声明
本文为[A research monk who doesn't like research]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231307030261.html
边栏推荐
- EMMC / SD learning notes
- ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
- AUTOSAR from introduction to mastery 100 lectures (81) - FIM of AUTOSAR Foundation
- [untitled] PID control TT encoder motor
- three. JS text ambiguity problem
- Subscribe to Alibaba demo send business messages
- Complete project data of UAV apriltag dynamic tracking landing based on openmv (LabVIEW + openmv + apriltag + punctual atom four axes)
- filter()遍历Array异常友好
- FFmpeg常用命令
- Learning notes of AMBA protocol
猜你喜欢
Summary of JVM knowledge points - continuously updated
安装nngraph
Customize classloader and implement hot deployment - use loadclass
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
MySQL 8.0.11下载、安装和使用可视化工具连接教程
Imx6ull QEMU bare metal tutorial 2: usdhc SD card
[untitled] PID control TT encoder motor
2020年最新字节跳动Android开发者常见面试题及详细解析
mui + hbuilder + h5api模拟弹出支付样式
PC starts multiple wechat at one time
随机推荐
Free and open source intelligent charging pile SaaS cloud platform of Internet of things
100 GIS practical application cases (34) - splicing 2020globeland30
Start mqbroker CMD failure resolution
Byte warehouse intern interview SQL questions
Custom nail robot alarm
4.22学习记录(你一天只做了水题是吗)
9419页最新一线互联网Android面试题解析大全
MySQL -- 16. Data structure of index
8086 of x86 architecture
ESP32 VHCI架构传统蓝牙设置scan mode,让设备能被搜索到
JDBC connection pool
Async void caused the program to crash
Nodejs + Mysql realize simple registration function (small demo)
Go language slicing operation
解决Oracle中文乱码的问题
(1) Openjuterpyrab comparison scheme
mui + hbuilder + h5api模拟弹出支付样式
Go language mapping operation
Translation of attention in natural language processing
这几种 VSCode 扩展是我最喜欢的