当前位置:网站首页>不会就坚持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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- First knowledge of C language (3)
- opengauss预检查安装
- SQL语句 关于字段转换怎么写
- GBase 8a特殊场景下屏蔽 ODBC 负载均衡方式?
- 小程序:区域滚动、下拉刷新、上拉加载更多
- Data mining -- Introduction to the basis of association analysis (Part 1)
- 数据挖掘——关联分析例题代码实现(下)
- 索引的最左前缀原理
- Object detection: object_ Detection API +ssd target detection model
- Is the array name a pointer
猜你喜欢
HCIP BGP
First knowledge of C language (3)
Design of environment detection system based on STM32 and Alibaba cloud
全屋WiFi方案:Mesh路由器组网和AC+AP
Problems encountered in vscode connection SSH
[Openstack] keystone,nova
数据挖掘——关联分析例题代码实现(下)
Object array merges elements according to a field
Lua语言(stm32+2G/4G模块)和C语言(stm32+esp8266)从字符串中提取相关数据的方法-整理
开课!看smardaten如何分解复杂业务场景
随机推荐
Leftmost prefix principle of index
Applet: Area scrolling, pull-down refresh, pull-up load more
Lucifer 98 life record ing
Asp. Net MVC, how can the controller in the folder jump to the controller in the root directory?
C language: getchar () and cache
Array as function parameter -- pointer constant / constant pointer
C语言实现三子棋游戏(详解)
Pointer constant and constant pointer
安装ros的laser_scan_matche库所遇到的问题(一)
[untitled]
Codeforces round 810 (Div. 2) d. rain (segment tree difference)
Three tier architecture of enterprise network
SQL window function
Is the array name a pointer
HCIP BGP
数据挖掘——关联分析基础介绍(上)
大佬们flink的JDBC SQL Connector现在不支持所有的数据库吗,例如vertica?
C language to achieve three chess game (detailed explanation)
Shielding ODBC load balancing mode in gbase 8A special scenarios?
[kvm] install KVM