-
Notifications
You must be signed in to change notification settings - Fork 0
/
LeetCode_215_3.java
36 lines (34 loc) · 1.07 KB
/
LeetCode_215_3.java
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
public class LeetCode_215_3 {
// 堆排序(大根堆)
class Solution {
public int findKthLargest(int[] nums, int k) {
heap_sort(nums);
for (int num : nums) {
System.out.println("num: " + num);
}
for (int i = 0; i < k - 1; i++) {
nums[0] = nums[nums.length - i - 1];
down(nums, 0, nums.length - i - 2);
}
return nums[0];
}
public void heap_sort(int[] nums) {
for (int i = nums.length / 2; i >= 0; i--) {
down(nums, i, nums.length - 1);
}
}
public void down(int[] nums, int u, int r) {
int t = u;
if (2 * u + 1 <= r && nums[2 * u + 1] > nums[t])
t = 2 * u + 1;
if (2 * u + 2 <= r && nums[2 * u + 2] > nums[t])
t = 2 * u + 2;
if (u != t) {
int temp = nums[u];
nums[u] = nums[t];
nums[t] = temp;
down(nums, t, r);
}
}
}
}