当前位置:网站首页>不会就坚持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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- Basic configuration of BGP - establish peers and route announcements
- The difference between dynamic, VaR and object in fluent
- The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
- "Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
- C language: getchar () and cache
- Wechat applet monitors sliding events on the screen
- BGP的基础配置---建立对等体、路由宣告
- Press the missing number of interview question 17.04 | | 260. the number that appears only once (including bit operation knowledge points)
- LCA board
- SQL time fuzzy query datediff() function
猜你喜欢

Compilation and linking

Ma Zhixing entered the mass production of front loading, starting with the self-developed domain controller?

通过js来实现一元二次方程的效果,输入a,b,c系数后可计算出x1和x2的值

全屋WiFi方案:Mesh路由器组网和AC+AP

Is the array name a pointer

Note: restframe work records many to one tables, how to serialize in that table (reverse query)

信号处理中的反傅里叶变换(IFFT)原理

Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes

When array is used as a function parameter, it is better to use the array size as a function parameter

HCIP BGP
随机推荐
为什么opengauss启动的时候这么多的unknown?
Safari's compatibility with Z-index
3. Solve pychart's error unresolved reference 'selenium' unresolved reference 'webdriver‘
Pointer constant and constant pointer
Data mining -- code implementation of association analysis example (Part 2)
Fuzzy query of SQL
优炫数据库有办法查到主集群每天传给备集群的日志量吗?
RMAN do not mark expired backups
【深度学习CPU(番外篇)——虚拟内存】
[deep learning CPU (part outside) - virtual memory]
The data source is SQL server. I want to configure the incremental data of the last two days of the date field updatedate to add
The structure pointer must be initialized, and the pointer must also be initialized
伏英娜:元宇宙就是新一代互联网!
"Weilai Cup" 2022 Niuke summer multi school training camp 1 J serval and essay (heuristic merger)
[untitled]
The output comparison function of Tim is introduced in detail through PWM breathing lamp and PWM controlled DC motor
SQL time fuzzy query datediff() function
SQL server当存储过程接收的参数是int类型时,如何做判断?
BIO、NIO、AIO的区别和原理
如何查询版本的提交号