-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKthLargestElementinanArray.java
More file actions
43 lines (40 loc) · 959 Bytes
/
KthLargestElementinanArray.java
File metadata and controls
43 lines (40 loc) · 959 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
package medium;
import java.util.Arrays;
/**
* ClassName: KthLargestElementinanArray.java
* Auther: chenyiAlone
* Create Time: 2019/5/9 18:25
* Description: No.215
* 思路:
* 1. sort array
* 2. return Kth largest elements
* 3. ps: not need to skip repeat elements
*
*
*
*
* Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
*
* Example 1:
*
* Input: [3,2,1,5,6,4] and k = 2
* Output: 5
* Example 2:
*
* Input: [3,2,3,1,2,4,5,5,6] and k = 4
* Output: 4
* Note:
* You may assume k is always valid, 1 ≤ k ≤ array's length.
*
*/
public class KthLargestElementinanArray {
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
int index = 1;
for (int i = nums.length - 1; i >= 0; i--) {
if (index++ == k)
return nums[i];
}
return 0;
}
}