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 ...
随机推荐
- SpringBoot使用logback日志记录
在resources里的配置文件: logback-spring.xml <?xml version="1.0" encoding="UTF-8" ?&g ...
- 解决 git branch -a 无法全部显示远程的分支,只显示master分支
新建分支 若遇到 git branch -a 无法全部显示远程的分支,只显示master分支 可以通过 git fetch 将本地远程跟踪分支进行更新,与远程分支保持一致
- hdu acm 2844 Coins 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2844 题目意思:有A1,A2,...,An 这 n 种面值的钱,分别对应的数量是C1,C2,...,C ...
- SPOJ:OR(位运算&数学期望)
Given an array of N integers A1, A2, A3…AN. If you randomly choose two indexes i ,j such that 1 ≤ i ...
- BZOJ_2186_[Sdoi2008]沙拉公主的困惑_欧拉函数
BZOJ_2186_[Sdoi2008]沙拉公主的困惑_欧拉函数 Description 大富翁国因为通货膨胀,以及假钞泛滥,政府决定推出一项新的政策:现有钞票编号范围为1到N的阶乘,但是,政府只发行 ...
- 【TJOI2013】 单词
[题目链接] 点击打开链接 [算法] AC自动机+递推 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 200 ...
- javascript switch..... case
switch(条件表达式) { case 常量: { 语句a; } break; case 常量: { 语句b; } break; case 常量: { 语句c; } break; ... case ...
- NYOJ5——Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述:Given two strings A and B, whose alph ...
- 利用ASP .NET Core的静态文件原理实现远程访问Nlog日志内容及解决遇到的坑
最近项目上试运行发现,很多时候网站出了问题或者某个功能不正常,常常需要运维人员去服务器里面查看一下日志,看看日志里面会产生什么异常,这样导致每次都要去远程服务器很不方便,有时服务器是客户保管的不能让我 ...
- 51nod1270 【dp】
思路: dp[i][0]代表第i个位置取1,dp[i][1]代表第i个位置取b[i]. #include <bits/stdc++.h> using namespace std; type ...