File tree Expand file tree Collapse file tree 1 file changed +18
-14
lines changed
solution/0200-0299/0215.Kth Largest Element in an Array Expand file tree Collapse file tree 1 file changed +18
-14
lines changed Original file line number Diff line number Diff line change 1
1
class Solution {
2
+ private int [] nums ;
3
+ private int k ;
4
+
2
5
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 );
5
9
}
6
10
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 ];
10
14
}
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 ];
13
17
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
+ }
18
22
if (i < j ) {
19
23
int t = nums [i ];
20
24
nums [i ] = nums [j ];
21
25
nums [j ] = t ;
22
26
}
23
27
}
24
28
if (j < k ) {
25
- return quickSort (nums , j + 1 , right , k );
29
+ return quickSort (j + 1 , r );
26
30
}
27
- return quickSort (nums , left , j , k );
31
+ return quickSort (l , j );
28
32
}
29
- }
33
+ }
You can’t perform that action at this time.
0 commit comments