-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha_215.java
More file actions
53 lines (50 loc) · 1.49 KB
/
a_215.java
File metadata and controls
53 lines (50 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package sort;
import java.util.PriorityQueue;
//数组中的第K个最大元素
public class a_215 {
public int findKthLargest(int[] nums, int k) {
//int index = k-1;
k = nums.length - k;
int lo = 0, hi = nums.length-1;
while (lo < hi) {//< ,<=则不会通过
int j = partition(nums, lo, hi);
if (j == k) {
break;
} else if (j < k) {
lo = j + 1;
} else {
hi = j - 1;
}
}
return nums[k];
}
private int partition(int[] nums ,int lo, int hi) {
int i = lo, j = hi+1;
int re = nums[lo];
while (true) {
while (nums[++i] < re && i < hi);//后面&&的条件不能省略
while (nums[--j] > re && j > lo);
if (i >= j) break;
swap(nums, i ,j);
}
swap(nums, lo, j);
return j;
}
private void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
class StackClass {
public int findKthLargest(int[] nums, int k) {
PriorityQueue<Integer> queue = new PriorityQueue<>();
for(int val : nums) {
queue.add(val);
if (queue.size() > k) {//维护堆的大小为k
queue.poll();//每次弹出删除堆中最小元素,最后只剩k个元素
}
}
return queue.peek();
}
}
}