Kth Largest Element in an Array
-问题描述-
-算法分析-
-代码实现-
问题描述
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.
For example,
Given [3,2,1,5,6,4] and k = 2, return 5.
算法分析
利用sort函数将数组进行排序,得到第K大的数。
代码实现
@requires_authorization
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
int n = nums.size();
sort(nums.begin(), nums.end());
return nums[n-k];
}
};