LeetCode-215. Kth Largest Element in an Array

网友投稿 530 2022-11-09

LeetCode-215. Kth Largest Element in an Array

LeetCode-215. 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

题解:

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:LeetCode-200. Number of Islands
下一篇:LeetCode-216. Combination Sum III
相关文章

 发表评论

暂时没有评论,来抢沙发吧~