当前位置:网站首页>不会就坚持70天吧 数组中第k大的数
不会就坚持70天吧 数组中第k大的数
2022-07-29 04:13:00 【一只小小明】
调用数组排序的Api
按升序排列,取倒数第k个元素。
时间复杂度:O(n*logn)
class Solution {
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length - k];
}
}
作者:fen-zi-yun-he
链接:https://leetcode.cn/problems/xx4gT2/solution/-by-fen-zi-yun-he-4qjy/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
使用优先队列PriorityQueue
PriorityQueue默认是升序排列,所以如果PriorityQueue中存放的数超过k个,就移除这堆头最小元素。
最后剩下的k个元素中的第一个元素,即为所求。
时间复杂度:O(n*logn),空间复杂度:O(n)
class Solution {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> pq = new PriorityQueue<>();
for(int i = 0; i < nums.length; i ++) {
pq.offer(nums[i]);
if(pq.size() > k) {
//如果堆存放数量超过k,移除堆头最小元素
pq.poll();
}
}
return pq.poll();
}
}
作者:fen-zi-yun-he
链接:https://leetcode.cn/problems/xx4gT2/solution/-by-fen-zi-yun-he-4qjy/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
手写归并排序
时间复杂度:O(n*logn),空间复杂度:O(n)
class Solution {
public int findKthLargest(int[] nums, int k) {
nums = queueSort(nums, 0, nums.length - 1);
return nums[nums.length - k];
}
private int[] queueSort(int[] nums, int left, int right) {
if(left == right) {
// 这里不是返回nums,而是返回一个长度为1的新数组
return new int[]{nums[left]};
}
int mid = left + (right - left >> 1);
int[] leftArr = queueSort(nums, left, mid);
int[] rightArr = queueSort(nums, mid + 1, right);
return merge(leftArr, rightArr);
}
private int[] merge(int[] left, int[] right) {
int len1 = left.length, len2 = right.length;
int[] tmp = new int[len1 + len2];
int i = 0, j = 0;
int idx = 0;
while(i < len1 && j < len2) {
if(left[i] < right[j]) {
tmp[idx ++] = left[i ++];
} else {
tmp[idx ++] = right[j ++];
}
}
while(i < len1) {
tmp[idx ++] = left[i ++];
}
while(j < len2) {
tmp[idx ++] = right[j ++];
}
return tmp;
}
}
作者:fen-zi-yun-he
链接:https://leetcode.cn/problems/xx4gT2/solution/-by-fen-zi-yun-he-4qjy/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
手写堆排序
时间复杂度:O(n*logn),空间复杂度:O(1)
class Solution {
public int findKthLargest(int[] nums, int k) {
for(int i = 0; i < nums.length; i ++) {
heapInsert(nums, i);
}
int size = nums.length;
if(k == 1) {
return nums[0];
} else {
for(int i = 0; i < k - 1; i ++) {
swap(nums, 0, -- size);
heapify(nums, 0, size);
}
return nums[0];
}
}
private void heapInsert(int[] nums, int index) {
while(nums[index] > nums[(index - 1) / 2]) {
swap(nums, index, (index - 1) / 2);
index = (index - 1) / 2;
}
}
private void heapify(int[] nums, int index, int size) {
int largest = 2 * index + 1;
// 这里不应该写成 index < size
while(largest < size) {
largest = largest + 1 < size && nums[largest + 1] > nums[largest] ? largest + 1 : largest;
largest = nums[index] > nums[largest] ? index : largest;
if(largest == index) {
break;
}
swap(nums, index, largest);
index = largest;
largest = 2 * index + 1;
}
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
作者:fen-zi-yun-he
链接:https://leetcode.cn/problems/xx4gT2/solution/-by-fen-zi-yun-he-4qjy/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- AssertionError(“Torch not compiled with CUDA enabled“)
- 力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
- Leftmost prefix principle of index
- BGP的基础配置---建立对等体、路由宣告
- How to solve the problem of store ranking?
- 优炫数据库有办法查到主集群每天传给备集群的日志量吗?
- How to query the submission number of a version
- Applet: Area scrolling, pull-down refresh, pull-up load more
- First knowledge of C language (3)
- Some problems about pointers
猜你喜欢

MPU6050

MySQL gets the maximum value record by field grouping

Lua language (stm32+2g/4g module) and C language (stm32+esp8266) methods of extracting relevant data from strings - collation

C语言实现三子棋游戏(详解)

Nacos registry

Compilation and linking

LDP -- label distribution protocol

rman不标记过期备份

HCIP BGP

3. Solve pychart's error unresolved reference 'selenium' unresolved reference 'webdriver‘
随机推荐
LCA 板子
(.*?) regular expression
数据挖掘——关联分析基础介绍(上)
LCA board
The solution of porting stm32f103zet6 program to c8t6+c8t6 download program flash timeout
Whole house WiFi solution: mesh router networking and ac+ap
Deep understanding of browser caching mechanism (HTTP)
C language: getchar () and cache
Taobao product details interface (product details page data interface)
信号处理中的反傅里叶变换(IFFT)原理
MySQL gets the maximum value record by field grouping
[Openstack] keystone,nova
开课!看smardaten如何分解复杂业务场景
编译与链接
力扣面试题17.04 消失的数字||260.只出现一次的数字(内含位运算知识点)
Pointer variables -printf%d and%p meaning
初识C语言(3)
Locally call tensorboard and Jupiter notebook on the server (using mobaxterm)
Asp.net MVC中文件夹中的控制器如何跳转到根目录的控制器中?
如何查询版本的提交号