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.
题意是找到第k个最大的元素,用最普通的选择法排序就可以AC,
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
for (int i=0; i<k; i++)
{
for(int j=i+1;j<nums.size(); j++)
{
if(nums[i]<nums[j])
{
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}
}
return nums[k-1];
}
};