Skip to content

Commit 51745e2

Browse files
committed
update 0215 Solution for Java
1 parent 64cf0cf commit 51745e2

File tree

1 file changed

+18
-11
lines changed
  • solution/0200-0299/0215.Kth Largest Element in an Array

1 file changed

+18
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
class Solution {
22
public int findKthLargest(int[] nums, int k) {
3-
return find(nums, 0, nums.length - 1, k - 1);
3+
return findKthLargest(nums, 0, nums.length - 1, nums.length - k);
44
}
55

6-
private int find(int[] nums, int left, int right, int k) {
7-
int i = left;
8-
int j = right;
9-
int index = nums[left];
6+
public int findKthLargest(int[] nums, int l, int r, int k) {
7+
int i = l, j = r;
8+
int temp = nums[i];
109
while (i < j) {
11-
while (i < j && index >= nums[j]) j--;
10+
while (i < j && nums[j] >= temp) {
11+
j--;
12+
}
1213
if (i < j) {
1314
nums[i++] = nums[j];
1415
}
15-
while (i < j && index <= nums[i]) i++;
16+
while (i < j && nums[i] <= temp) {
17+
i++;
18+
}
1619
if (i < j) {
1720
nums[j--] = nums[i];
1821
}
1922
}
20-
nums[i] = index;
21-
if (i > k) return find (nums, left, i - 1, k);
22-
else if (i < k) return find(nums, i + 1, right, k);
23-
else return nums[i];
23+
nums[i] = temp;
24+
if (i == k) {
25+
return nums[i];
26+
} else if (i < k) {
27+
return findKthLargest(nums, i + 1, r, k);
28+
} else {
29+
return findKthLargest(nums, l, i - 1, k);
30+
}
2431
}
2532
}

0 commit comments

Comments
 (0)