lc 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.

multiset Accepted

投机取巧的方法,利用stl中multiset的特性,自动实现从小到大的排序,并且允许有相同元素的存在,但是要注意,multiset不能用下标获取元素,所以需删除之前不必要的元素,用*ans.begin()的方法获取首元素。

class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
multiset<int> ans;
for (auto i : nums) {
ans.insert(i);
}
while (ans.size() > k) ans.erase(ans.begin());
return *ans.begin();
}
};

分治 Accepted

类似于快排的原理,其中也蕴含了分治的思想。

class Solution {
public:
void swap(vector<int>& nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
} int findKthLargest(vector<int>& nums, int k) {
int n = nums.size();
int p = quick(nums, 0, n-1, n-k+1);
return nums[p];
} int quick(vector<int>& a, int low, int high, int k) {
int i = low, j = high, pivot = a[high];
while (i < j) {
if (a[i++] > pivot) swap(a, --i, --j);
}
swap(a, i, high);
int m = i - low + 1;
if (m == k) return i;
else if (m > k) return quick(a, low, i - 1, k);
else return quick(a, i + 1, high, k - m);
}
};

将递归转化成递推

思想和上面的方法是一样的,但是将递归转化成递推。

void swap(vector<int>& nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
} int findKthLargest(vector<int>& nums, int k) {
k = nums.size() - k;
int l = 0, r = nums.size() - 1;
while (l <= r) {
int i = l;
for (int j = l + 1; j <= r; j++)
if (nums[j] < nums[l]) swap(nums, j, ++i);
swap(nums, l, i); if (k < i) r = i - 1;
else if (k > i) l = i + 1;
else return nums[i];
}
return -1;
}

LN : leetcode 215 Kth Largest Element in an Array的更多相关文章

  1. 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)

    注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...

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

    传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...

  3. [LeetCode] 215. 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 so ...

  4. 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 so ...

  5. Java for 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 so ...

  6. [leetcode]215. 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 so ...

  7. C#版 - Leetcode 215. Kth Largest Element in an Array-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  8. 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 A ...

  9. 【LeetCode】215. Kth Largest Element in an Array (2 solutions)

    Kth Largest Element in an Array Find the kth largest element in an unsorted array. Note that it is t ...

随机推荐

  1. Codeforces Round #352 (Div. 2) C. Recycling Bottles

      C. Recycling Bottles time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  2. let 命令 与 var的区别

    ES6 新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. <script> { let a = 10; var b = 1; } ...

  3. HDU - 1874 畅通工程续(最短路径)

    d.已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离. s.最短路径 c.Dijkstra单源最短路 /* Dijkstra单源最短路 权值必须是非负 单源最短路径,Dijkstra算法 ...

  4. java回调机制及其实现

    1. 什么是回调函数 回调函数,顾名思义,用于回调的函数.回调函数只是一个功能片段,由用户按照回调函数调用约定来实现的一个函数.回调函数是一个工作流的一部分,由工作流来决定函数的调用(回调)时机.回调 ...

  5. maven实战(5)-- settings.xml的配置

    哈哈 查看maven的官方文档最权威:http://maven.apache.org/settings.html

  6. ios 图片拉伸方法

     前提:要注意图片的size和展示的图片view的size的大小. 假如图片高度50,展示图片view的高度30,拉伸会变成剪切. 如果图片尺寸不对,可以用mac自带的图片编辑器修改大小: 双击打开图 ...

  7. 在Service里调用AlertDialog

    用常规的方法在AlertDialog的时候,会报错,大意是「can not add window in this view」. 原因是Service是没有界面的,只有Activity才能添加界面. 解 ...

  8. [SoapUI] Jenkins 配置不同环境(TP, LIVE)

  9. [Selenium] 如何使用Chrome Options 定制测试Chrome 浏览器的特定属性 (类似FirefoxProfiles)

    Chrome Options 类似于FirefoxProfiles,用于定制待测试的Chrome 浏览器的特定属性 1)如果希望测试某个浏览器插件,可通过addExtensions方式提前加载以.cr ...

  10. Watir: 如果安装sougou浏览器,有可能导致Watir不能工作

    如果安装了搜狗,Watir::Browser.new并不一定能打开新的IE浏览器.这种情况下,必须卸载搜狗浏览器,当然,attach,find方法还是可以用的