当前位置:网站首页>不会就坚持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)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
边栏推荐
- 如何查询版本的提交号
- Data mining -- code implementation of association analysis example (Part 2)
- Beginner: array & String
- Is there any way for Youxuan database to check the log volume that the primary cluster transmits to the standby cluster every day?
- 伏英娜:元宇宙就是新一代互联网!
- 安装ros的laser_scan_matche库所遇到的问题(一)
- Who can elaborate on the semi consistent read under mysqlrc and how to reduce the deadlock probability?
- After I get the winfrom specific control ID from the database, I need to find the corresponding control through this ID and assign a value to the text text of the control. What should I do
- 2021 sist summer camp experience + record post of School of information, Shanghai University of science and technology
- Blood cases caused by < meta charset=UTF-8> -- Analysis of common character codes
猜你喜欢

Code or script to speed up the video playback of video websites

为什么opengauss启动的时候这么多的unknown?
![[Openstack] keystone,nova](/img/de/70b654a29a813c8fe828c4018bd4e7.png)
[Openstack] keystone,nova

Big manufacturers finally can't stand "adding one second", and companies such as Microsoft, Google meta propose to abolish leap seconds

SVG--loading动画

Lua语言(stm32+2G/4G模块)和C语言(stm32+esp8266)从字符串中提取相关数据的方法-整理

Data mining -- code implementation of association analysis example (Part 2)

Beginner: array & String

Simple cases of inner connection and left connection

Is the array name a pointer
随机推荐
Beginner: array & String
Pointer constant and constant pointer
Compilation and linking
Change the value of the argument by address through malloc and pointer
Value transmission and address transmission of C language, pointer of pointer
Who can elaborate on the semi consistent read under mysqlrc and how to reduce the deadlock probability?
Data mining -- code implementation of association analysis example (Part 2)
Differences and principles of bio, NiO and AIO
C language: getchar () and cache
编译与链接
Problems encountered in vscode connection SSH
全屋WiFi方案:Mesh路由器组网和AC+AP
Svg -- loading animation
Opengauss pre check installation
[deep learning CPU (part outside) - virtual memory]
About the writing of ALV format control part
Methods of using multiple deformations on an element
Pat a1069/b1019 the black hole of numbers
MySQL第三篇
如何查询版本的提交号