Solution 1: 自己写的
O(nk), Runtime: 126 ms, beats 1.73% submissions.
public class Solution {
public int findKthLargest(int[] nums, int k) {
for(int i = 0; i < k; i++) {
int tmp = 0;
for(int j = i+1; j < nums.length; j++) {
if(nums[j] > nums[i]) {
tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}
}
return nums[k-1];
}
}
Solution 2: Naive. 居然也过了
Runtime: 5ms
public class Solution {
public int findKthLargest(int[] nums, int k) {
Arrays.sort(nums);
return nums[nums.length-k];
}
}