LN : leetcode 215 Kth Largest Element in an Array
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的更多相关文章
- 剑指offer 最小的k个数 、 leetcode 215. Kth Largest Element in an Array 、295. Find Median from Data Stream(剑指 数据流中位数)
注意multiset的一个bug: multiset带一个参数的erase函数原型有两种.一是传递一个元素值,如上面例子代码中,这时候删除的是集合中所有值等于输入值的元素,并且返回删除的元素个数:另外 ...
- 网易2016 实习研发工程师 [编程题]寻找第K大 and leetcode 215. Kth Largest Element in an Array
传送门 有一个整数数组,请你根据快速排序的思路,找出数组中第K大的数. 给定一个整数数组a,同时给定它的大小n和要找的K(K在1到n之间),请返回第K大的数,保证答案存在. 测试样例: [1,3,5, ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- C#版 - Leetcode 215. Kth Largest Element in an Array-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- 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 ...
- 【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 ...
随机推荐
- linux初级学习笔记八:linux权限管理及权限管理命令详解!(视频序号:04_2)
本节学习的命令:chown,chgrp,chmod,openssl,umask 本节学习的技能: 文件权限详解及对其的操作 生成随机密码命令 用遮罩码对用户权限进行修改 站在用户登陆的角度来说SHEL ...
- hadoop-3.0.0 配置中的 yarn.nodemanager.aux-services 项
在hadoop-3.0.0-alpha4 的配置中,yarn.nodemanager.aux-services项的默认值是“mapreduce.shuffle”,但如果在hadoop-2.2 中继续使 ...
- windows下安装composer流程
1.php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" 2. php -r &quo ...
- [Selenium] 测试机器上安装了多个Firefox,如何指定运行哪一个?
可通过FirefoxBinary 来指定运行某个路径下的Firefox, 示例代码如下: public class testFirefoxBinary{ public static void main ...
- macbook pro 自带和用户后装的jdk的路径
苹果系统已经包含完整的J2SE,其中就有JDK和JVM(苹果叫VM).当然如果要升级JDK,那当然要自己下载安装了. 在MAC系统中,jdk的安装路径与windows不同,默认目录是:/System/ ...
- Luogu网校听课笔记(自用
TG助学课——noilinux 8.2 深夜 Noi Linux的使用——darkflames的博客 #include<bits/stdc++.h> using namespace std ...
- 【BZOJ 2721】 樱花
[题目链接] 点击打开链接 [算法] 令n!=z,因为1 / x + 1 / y = 1 / z,所以x,y>z,不妨令y = z + d 则1 / x + 1 / (z + d) = 1 / ...
- Watir: 应用Watir-Webdriver 访问需要证书的网站情况
#Suppose we will access an SVN net require 'watir-webdriver' b = Watir::Browser.new :chrome b.goto ' ...
- 微信小程序 WXML、WXSS 和JS介绍及详解
前几天折腾了下.然后列出一些实验结果,供大家参考. 百牛信息技术bainiu.ltd整理发布于博客园 0. 使用开发工具模拟的和真机差异还是比较大的.也建议大家还是真机调试比较靠谱. 1. WXML( ...
- backface-visibility
浏览器支持 只有 Internet Explorer 10+ 和 Firefox 支持 backface-visibility 属性. Opera 15+.Safari 和 Chrome 支持替代的 ...