215. Kth Largest Element in an Array

网友投稿 593 2022-09-04

215. Kth Largest Element in an Array

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.

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.

class Solution { public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); return nums[nums.length - k]; }}

class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: return (sorted(nums)[-k])

/** * @param {number[]} nums * @param {number} k * @return {number} */var findKthLargest = function(nums, k) { return nums.sort((a,b) => b-a)[k-1];};

class Solution: def findKthLargest(self, nums: List[int], k: int) -> int: heapify(nums) return nlargest(k, nums)[-1]

/** * @param {number[]} nums * @param {number} k * @return {number} */var findKthLargest = function(nums, k) { var num = nums.sort((a,b) => a - b) return num[nums.length - k]};

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

上一篇:Stanford cars-数据集
下一篇:数据库索引是什么?新华字典来帮你!(数据库里的索引是什么意思)
相关文章

 发表评论

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