[LeetCode] Kth Largest Element in an Array 数组中第k大的数字

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.

这道题让我们求数组中第k大的数字,怎么求呢,当然首先想到的是给数组排序,然后求可以得到第k大的数字。先看一种利用c++的STL中的集成的排序方法,不用我们自己实现,这样的话这道题只要两行就完事了,代码如下:

解法一

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

参考资料:

https://leetcode.com/discuss/37724/solutions-nlogn-sort-klogn-heapsort-average-quicksort-kind

http://blog.csdn.net/kangrydotnet/article/details/45973575

时间: 2024-10-11 22:45:52

[LeetCode] Kth Largest Element in an Array 数组中第k大的数字的相关文章

LeetCode | 0215. Kth Largest Element in an Array数组中的第K个最大元素【Python】

LeetCode 0215. Kth Largest Element in an Array数组中的第K个最大元素[Medium][Python][快排][堆] Problem LeetCode 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:

215 Kth Largest Element in an Array 数组中的第K个最大元素

在未排序的数组中找到第 k 个最大的元素.请注意,它是数组有序排列后的第 k 个最大元素,而不是第 k 个不同元素.例如,给出 [3,2,1,5,6,4] 和 k = 2,返回 5.注意事项:你可以假设 k 总是有效的,1 ≤ k ≤ 数组的长度. 详见:https://leetcode.com/problems/kth-largest-element-in-an-array/description/ 方法一: class Solution { public: int findKthLarges

leetcode Kth Largest Element in an Array

题目连接 https://leetcode.com/problems/kth-largest-element-in-an-array/ Kth Largest Element in an Array Description 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 eleme

LeetCode Kth Largest Element in an Array (快速排序)

题意: 在一个无序的数组中第k大的数是多少? 思路: 按照快排的思路,如果每次分成两段后,设为L和R.如果R>=k ,则答案在右边集合,否则在左边集合. 这里用了3位取中法.注意快排别给写死循环了. 1 class Solution { 2 public: 3 int findKthLargest(vector<int>& nums, int k) { 4 if(k>nums.size()) return 0; 5 return DFS(nums,0,nums.size()

LeetCode——Kth Largest Element in an Array

Description: 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. 你确定你不是在逗我? public class Solution { public

LeetCode:Kth Largest Element in an Array(need update)

problem: 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

LeetCode OJ 215. Kth Largest Element in an Array 堆排序求解

题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/ 215. Kth Largest Element in an Array My Submissions Question Total Accepted: 43442 Total Submissions: 136063 Difficulty: Medium Find the kth largest element in an unsorted array. Not

网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array

传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5,2,2],5,3 返回:2 note: 注意手写快排的时候: while(i < j) { while(j > i && a[j] > a[left]) j--; while(i < j && a[i] <= a[left]) i++; if(i

leetcode 215. Kth Largest Element in an Array 寻找第k个大的数---------- java

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