forked from yubinbai/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
50 lines (46 loc) · 1.11 KB
/
Solution.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Solution {
public int findKthLargest(int[] nums, int k) {
return findKthLargest(nums, k, 0, nums.length - 1);
}
private int findKthLargest(int[] nums, int k, int low, int high) {
int p = partition(nums, low, high);
if (p == high - k + 1) {
return nums[p];
} else if (p > high - k + 1) {
return findKthLargest(nums, k - (high - p + 1), low, p - 1);
} else {
return findKthLargest(nums, k, p + 1, high);
}
}
int partition(int a[], int l, int h) {
int x = a[h];
int i = (l - 1);
for (int j = l; j < h; j++) {
if (a[j] < x) {
i++;
swap(a, i, j);
}
}
swap(a, i + 1, h);
return (i + 1);
}
void swap(int[] a, int i, int j) {
int swap = a[i];
a[i] = a[j];
a[j] = swap;
}
public static void main(String[] args) {
Solution s = new Solution();
int[] nums;
int k;
nums = new int[] {3, 2, 1, 5, 6, 4};
k = 2;
System.out.println(s.findKthLargest(nums, k));
nums = new int[] {3, 2, 1, 5, 6, 4};
k = 1;
System.out.println(s.findKthLargest(nums, k));
nums = new int[] {3, 2, 1, 5, 6, 4};
k = 4;
System.out.println(s.findKthLargest(nums, k));
}
}