Skip to content

Commit ff0882c

Browse files
authored
Update Solution.java
1 parent 1a0b3e3 commit ff0882c

File tree

1 file changed

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

1 file changed

+18
-14
lines changed
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
class Solution {
2+
private int[] nums;
3+
private int k;
4+
25
public int findKthLargest(int[] nums, int k) {
3-
int n = nums.length;
4-
return quickSort(nums, 0, n - 1, n - k);
6+
this.nums = nums;
7+
this.k = nums.length - k;
8+
return quickSort(0, nums.length - 1);
59
}
610

7-
private int quickSort(int[] nums, int left, int right, int k) {
8-
if (left == right) {
9-
return nums[left];
11+
private int quickSort(int l, int r) {
12+
if (l == r) {
13+
return nums[l];
1014
}
11-
int i = left - 1, j = right + 1;
12-
int x = nums[(left + right) >>> 1];
15+
int i = l - 1, j = r + 1;
16+
int x = nums[(l + r) >>> 1];
1317
while (i < j) {
14-
while (nums[++i] < x)
15-
;
16-
while (nums[--j] > x)
17-
;
18+
while (nums[++i] < x) {
19+
}
20+
while (nums[--j] > x) {
21+
}
1822
if (i < j) {
1923
int t = nums[i];
2024
nums[i] = nums[j];
2125
nums[j] = t;
2226
}
2327
}
2428
if (j < k) {
25-
return quickSort(nums, j + 1, right, k);
29+
return quickSort(j + 1, r);
2630
}
27-
return quickSort(nums, left, j, k);
31+
return quickSort(l, j);
2832
}
29-
}
33+
}

0 commit comments

Comments
 (0)