[算法分析与设计] leetcode 每周一题: 215. Kth Largest Element in an Array
2017-09-18 15:36
549 查看
题目链接:https://leetcode.com/problems/kth-largest-element-in-an-array/description/
题目大意:找出数组中第N大的数字
思路:
1.一开始,我是单纯采用算法概论里面的分治策略解答,将数组每次划分为3类(大于目标值,小于目标值,等于目标值),当递归到N处于等于目标值数组时候,即答案,结果,leetcode中超出内存限制,代码如下:
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
vector<int> larger, smaller, same;
int target = nums[0];
for(auto i : nums) {
if(i < target)
smaller.push_back(i);
else if(i == target)
same.push_back(i);
else
larger.push_back(i);
}
if(k <= larger.size()) {
return findKthLargest(larger, k);
}
else if(k <= (larger.size() + same.size()))
return target;
else
return findKthLargest(smaller, k - (larger.size() + same.size()));
}
};后来,网上查了其他博客,发现可以用类似快排的方法解答,代码如下;
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
int len = nums.size();
Quick_sort(nums,k-1,0,len-1);
return nums[k-1];
}
void Quick_sort(vector<int>& nums, int k, int left, int right) {
int l = left;
int r = right;
// int len = nums.size();
int key = nums[l];
while(l < r) {
while(r > l && nums[r] < key)
r--;
nums[l] = nums[r];
while(r > l && nums[l] >= key)
l++;
nums[r] = nums[l];
}
nums[l] = key;
if(k < r)
Quick_sort(nums,k,left,r-1);
if(k > r)
Quick_sort(nums,k,r+1,right);
}
};
题目大意:找出数组中第N大的数字
思路:
1.一开始,我是单纯采用算法概论里面的分治策略解答,将数组每次划分为3类(大于目标值,小于目标值,等于目标值),当递归到N处于等于目标值数组时候,即答案,结果,leetcode中超出内存限制,代码如下:
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
vector<int> larger, smaller, same;
int target = nums[0];
for(auto i : nums) {
if(i < target)
smaller.push_back(i);
else if(i == target)
same.push_back(i);
else
larger.push_back(i);
}
if(k <= larger.size()) {
return findKthLargest(larger, k);
}
else if(k <= (larger.size() + same.size()))
return target;
else
return findKthLargest(smaller, k - (larger.size() + same.size()));
}
};后来,网上查了其他博客,发现可以用类似快排的方法解答,代码如下;
class Solution {
public:
int findKthLargest(vector<int>& nums, int k) {
int len = nums.size();
Quick_sort(nums,k-1,0,len-1);
return nums[k-1];
}
void Quick_sort(vector<int>& nums, int k, int left, int right) {
int l = left;
int r = right;
// int len = nums.size();
int key = nums[l];
while(l < r) {
while(r > l && nums[r] < key)
r--;
nums[l] = nums[r];
while(r > l && nums[l] >= key)
l++;
nums[r] = nums[l];
}
nums[l] = key;
if(k < r)
Quick_sort(nums,k,left,r-1);
if(k > r)
Quick_sort(nums,k,r+1,right);
}
};
相关文章推荐
- 【算法分析与设计】【第三周】215. Kth Largest Element in an Array
- leetcode:Kth Largest Element in an Array 使用快速选择算法,以及对其复杂度的分析
- 【LeetCode】215. Kth Largest Element in an Array 解题报告
- 算法课第16周第1题—— 215. Kth Largest Element in an Array
- [leetcode] 215. Kth Largest Element in an Array
- 【LeetCode】 215. Kth Largest Element in an Array
- leetcode215. Kth Largest Element in an Array
- LeetCode 215. Kth Largest Element in an Array
- leetcode 215. Kth Largest Element in an Array 堆排序的简单应用 + 快速排序
- leetcode 215. Kth Largest Element in an Array
- LeetCode-215. Kth Largest Element in an Array
- Leetcode 215. Kth Largest Element in an Array
- [leetcode]215. Kth Largest Element in an Array
- Leetcode 215. Kth Largest Element in an Array
- 【leetcode】215. Kth Largest Element in an Array
- LeetCode 215. Kth Largest Element in an Array
- leetcode- 215. Kth Largest Element in an Array(基于快速排序思想)
- leetcode刷题之旅——215. Kth Largest Element in an Array
- 算法分析与设计丨第二周丨LeetCode(3)——Kth Largest Element in an Array(Medium)
- leetcode 215. Kth Largest Element in an Array