-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution215_3.java
More file actions
46 lines (42 loc) · 968 Bytes
/
Copy pathsolution215_3.java
File metadata and controls
46 lines (42 loc) · 968 Bytes
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
public class solution215_3 {
public int findKthLargest(int[] nums, int k) {
k = nums.length - k;
int l = 0;
int h = nums.length - 1;
while(l<h)
{
int j = partition(nums,l,h);
if(j == k)
{
break;
}else if(j < k)
{
l = j+1;
}else {
h = j - 1;
}
}
return nums[k];
}
private int partition(int[] nums, int l, int h) {
int i = l;
int j = h+1;
while(true)
{
while (nums[++i]<nums[l]&&i<h);
while (nums[--j]>nums[l]&&j>l);
if(i>=j)
{
break;
}
swap(nums,i,j);
}
swap(nums,l,j);
return j;
}
private void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}