[leetcode] 第三周作业
2017-09-21 13:43
281 查看
题目来源: 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.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
分析:
解法一
直接排序:排序后直接返回第k大的数,最直接也是最差的一种解法
解法二
快排划界:最常用的一种解法
将要划界的数组段末尾的元素为划界元,将比其小的数交换至前,比其大的数交换至后,最后将划界元放在“中间位置”(左边小,右边大)。划界将数组分解成两个子数组(可能为空)。
设数组下表从low开始,至high结束。
1、 总是取要划界的数组末尾元素为划界元x,开始划界:
a) 用j从low遍历到high-1(最后一个暂不处理),i=low-1,如果nums[j]比x小就将nums[++i]与nums[j]交换位置
b) 遍历完后再次将nums[i+1]与nums[high]交换位置(处理最后一个元素);
c) 返回划界元的位置i+1,下文称其为midpos
这时的midpos位置的元素,此时就是整个数组中第N-midpos大的元素,我们所要做的就像二分法一样找到K=N-midpos的“中间位置”,即midpos=N-K
2、 如果midpos==n-k,那么返回该值,这就是第k大的数。
3、 如果midpos>n-k,那么第k大的数在左半数组
4、 如果midpos
更多分治算法讲解:
http://blog.csdn.net/zwhlxl/article/details/44086105
题目:
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.
Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.
分析:
解法一
直接排序:排序后直接返回第k大的数,最直接也是最差的一种解法
class Solution { public: int findKthLargest(vector<int>& nums, int k) { sort(nums.begin(),nums.end()); return nums[nums.size()-k]; } };
解法二
快排划界:最常用的一种解法
将要划界的数组段末尾的元素为划界元,将比其小的数交换至前,比其大的数交换至后,最后将划界元放在“中间位置”(左边小,右边大)。划界将数组分解成两个子数组(可能为空)。
设数组下表从low开始,至high结束。
1、 总是取要划界的数组末尾元素为划界元x,开始划界:
a) 用j从low遍历到high-1(最后一个暂不处理),i=low-1,如果nums[j]比x小就将nums[++i]与nums[j]交换位置
b) 遍历完后再次将nums[i+1]与nums[high]交换位置(处理最后一个元素);
c) 返回划界元的位置i+1,下文称其为midpos
这时的midpos位置的元素,此时就是整个数组中第N-midpos大的元素,我们所要做的就像二分法一样找到K=N-midpos的“中间位置”,即midpos=N-K
2、 如果midpos==n-k,那么返回该值,这就是第k大的数。
3、 如果midpos>n-k,那么第k大的数在左半数组
4、 如果midpos
//快排划界,如果划界过程中当前划界元的中间位置就是k则找到了 class Solution { public: int quickPartion(vector<int> &vec, int low,int high) { int x = vec[high]; int i = low - 1; for (int j = low; j <= high - 1; j++) { if (vec[j] <= x) swap(vec,++i,j); } swap(vec,++i,high); return i; } void swap(vector<int>& nums, int i, int j){ int temp = nums[i]; nums[i]=nums[j]; nums[j]=temp; } int getQuickSortK(vector<int> &vec, int low,int high, int k) { if(low >= high) return vec[low]; int midpos = quickPartion(vec, low,high); //对原数组vec[low]到vec[high]的元素进行划界 if (midpos == vec.size() - k) return vec[midpos]; else if (midpos < vec.size() - k) return getQuickSortK(vec, midpos+1, high, k); else return getQuickSortK(vec, low, midpos-1, k); } int findKthLargest(vector<int>& nums, int k) { return getQuickSortK(nums,0,nums.size()-1,k); } };
更多分治算法讲解:
http://blog.csdn.net/zwhlxl/article/details/44086105
相关文章推荐
- 第三周作业2(LeetCode75)
- 第三周作业1(LeetCode53)
- Leetcode 200.Number of Islands(第三周作业)
- 算法课程Leetcode作业第三周技术博客
- 第三周作业1(LeetCode53)
- 斯坦福机器学习Coursera课程:第三周作业--逻辑回归
- 马哥2016全新Linux+Python高端运维班第三周作业作答
- 20155311高梓云的第三周作业
- 软件工程专业导论第三周作业
- 【算法作业10】LeetCode 53. Maximum Subarray
- 【第三周】第六章作业
- 第三周作业
- 第三周作业
- Java-MOOC第三周作业
- 算法课程Leetcode作业第二周技术博客
- 第三周编程作业2 数字特征值
- 第三周作业
- 《软件工程》第三周 作业(二)
- 第三周作业
- 第三周作业