#Leetcode# 215. Kth Largest Element in an Array

六月ゝ 毕业季﹏ 提交于 2020-03-29 18:06:27

https://leetcode.com/problems/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.

Example 1:

Input: [3,2,1,5,6,4] and k = 2
Output: 5

Example 2:

Input: [3,2,3,1,2,4,5,5,6] and k = 4
Output: 4

Note: 
You may assume k is always valid, 1 ≤ k ≤ array's length.

代码:

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        int n = nums.size();
        int ans;
        sort(nums.begin(), nums.end());
        ans = nums[n - k];
        return ans;
    }
    
};

  FHFHFH

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!