Find the kth largest element in an unsorted array. Note that it was the kth largest element in the sorted order and not the kth distinct element.
For example,
Given [3,2,1,5,6,4]
and k = 2, return 5.
Note:
You may assume k are always valid, 1≤k≤array ' s length.
This problem let us find the array of the number k, how to ask, of course, the first thought is to sort the array, and then ask to get the number K large. First look at a use of C + + STL in the integration of the sorting method, without our own implementation, so that the problem as long as two lines will be finished, the code is as follows:
Solution One
class Solution {public: int findkthlargest (vector<intint k) { sort (nums.begin (), Nums.end ()); return nums[nums.size ()- K]; };
Resources:
Https://leetcode.com/discuss/37724/solutions-nlogn-sort-klogn-heapsort-average-quicksort-kind
http://blog.csdn.net/kangrydotnet/article/details/45973575
[Leetcode] Kth largest Element in an array of K-large numbers