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.
Note:
You may assume k is always valid, 1 ≤ k ≤ array's length.
解析:使用冒泡排序,将向量里面的值从大到小排序,取出第k个元素。
源代码如下:
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
int len=nums.size();
for(int i=0;i<len;i++)
for(int j=0;j<len-1;j++)
{
if(nums[j]<nums[j+1])
{
int temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
return nums[k-1];
}
};