[Leetcode] Kth largest Element in an array of K-large numbers

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.