[leetcode] median of two sorted arrays[C++ vector版本]
来源:互联网 发布:overlay网络 是什么 编辑:程序博客网 时间:2024/05/17 09:38
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3]nums2 = [2]The median is 2.0
Example 2:
nums1 = [1, 2]nums2 = [3, 4]The median is (2 + 3)/2 = 2.5
思路:
原文用英文进行解释,在此我们将其翻译成汉语。该方法的核心是将原问题转变成一个寻找第k小数的问题(假设两个原序列升序排列),这样中位数实际上是第(m+n)/2小的数。所以只要解决了第k小数的问题,原问题也得以解决。
首先假设数组A和B的元素个数都大于k/2,我们比较A[k/2-1]和B[k/2-1]两个元素,这两个元素分别表示A的第k/2小的元素和B的第k/2小的元素。这两个元素比较共有三种情况:>、<和=。如果A[k/2-1]<B[k/2-1],这表示A[0]到A[k/2-1]的元素都在A和B合并之后的前k小的元素中。换句话说,A[k/2-1]不可能大于两数组合并之后的第k小值,所以我们可以将其抛弃。(但是不能此时认为B[k/2-1]就是中位数,因为A[k/2]后面的数可能还是小于B[B[k/2-1]],举例:A=[1,2,3,4,5] B=[1,100,101,102])
证明也很简单,可以采用反证法。假设A[k/2-1]大于合并之后的第k小值,我们不妨假定其为第(k+1)小值。由于A[k/2-1]小于B[k/2-1],所以B[k/2-1]至少是第(k+2)小值。但实际上,在A中至多存在k/2-1个元素小于A[k/2-1],B中也至多存在k/2-1个元素小于A[k/2-1],所以小于A[k/2-1]的元素个数至多有k/2+ k/2-2,小于k,这与A[k/2-1]是第(k+1)的数矛盾。
当A[k/2-1]>B[k/2-1]时存在类似的结论。
当A[k/2-1]=B[k/2-1]时,我们已经找到了第k小的数,也即这个相等的元素,我们将其记为m。由于在A和B中分别有k/2-1个元素小于m,所以m即是第k小的数。(这里可能有人会有疑问,如果k为奇数,则m不是中位数。这里是进行了理想化考虑,在实际代码中略有不同,是先求k/2,然后利用k-k/2获得另一个数。)
通过上面的分析,我们即可以采用递归的方式实现寻找第k小的数。此外我们还需要考虑几个边界条件:
- 如果A或者B为空,则直接返回B[k-1]或者A[k-1];
- 如果k为1,我们只需要返回A[0]和B[0]中的较小值;
- 如果A[k/2-1]=B[k/2-1],返回其中一个;
最终实现的代码为:
C++版本:
class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { int n=nums1.size(),m=nums2.size(); if((n+m)%2){ return (double) findK(nums1,0,n-1,nums2,0,m-1,(n+m)/2+1); }else { return (double) (findK(nums1,0,n-1,nums2,0,m-1,(n+m)/2)+findK(nums1,0,n-1,nums2,0,m-1,(n+m)/2+1))/2; } } int findK(vector<int>& nums1, int a, int b, vector<int>& nums2, int c, int d, int k){ if (b - a>d - c) return findK(nums2, c, d, nums1, a, b, k); if (b<a) return nums2[k - 1]; if (k == 1) return min(nums1[a], nums2[c]); int x = min(k / 2, b - a + 1); int y = k - x; if (nums1[a + x - 1] == nums2[c + y - 1]){ return nums1[a + x - 1]; } else if (nums1[a + x - 1]<nums2[c + y - 1]){ return findK(nums1, a + x, b, nums2, c, d, k - x); } else { return findK(nums1, a , b, nums2, c + y, d, k - y); } }};
C语言版本:明显利用数组指针的形式要比vector简洁。
double findKth(int a[], int m, int b[], int n, int k) { //always assume that m is equal or smaller than n if (m > n) return findKth(b, n, a, m, k); if (m == 0) return b[k - 1]; if (k == 1) return min(a[0], b[0]); //divide k into two parts int pa = min(k / 2, m), pb = k - pa; if (a[pa - 1] < b[pb - 1]) return findKth(a + pa, m - pa, b, n, k - pa); else if (a[pa - 1] > b[pb - 1]) return findKth(a, m, b + pb, n - pb, k - pb); else return a[pa - 1]; } class Solution { public: double findMedianSortedArrays(int A[], int m, int B[], int n) { int total = m + n; if (total & 0x1) return findKth(A, m, B, n, total / 2 + 1); else return (findKth(A, m, B, n, total / 2) + findKth(A, m, B, n, total / 2 + 1)) / 2; } };
- [leetcode] median of two sorted arrays[C++ vector版本]
- leetcode Median of Two Sorted Arrays (C)
- [LeetCode]2 Median of Two Sorted Arrays (C++,Python实现)
- [leetcode-4]Median of Two Sorted Arrays(c)
- [c++]LeetCode Median of Two Sorted Arrays问题
- [LeetCode#4][C]Median of Two Sorted Arrays
- LeetCode 4. Median of Two Sorted Arrays(c++)
- leetcode 4 Median of Two Sorted Arrays c语言实现
- 【leetcode】4. Median of Two Sorted Arrays(Python & C++)
- LeetCode-4-Median of Two Sorted Arrays(C语言实现)
- LeetCode: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [Leetcode] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- [LeetCode]Median of Two Sorted Arrays
- leetcode Median of Two Sorted Arrays
- leetcode.array--39. Combination Sum
- win10 CPU占用率过高 经常100%
- 【DP】【线段树】【树状数组】saber 题解
- 微信小程序如何判断系统并定制不一样的功能
- Java笔记之循环结构
- [leetcode] median of two sorted arrays[C++ vector版本]
- 剑指Offer——“你最大的缺点是什么”回答技巧及范例
- 手动编写Servlet
- 51 nod 1378 夹克老爷的愤怒 树形dp + 贪心
- saturate_cast<>()
- qt5.5 webview无法显示百度网页等
- react-native学习之路-安装开源的tab-navigator出错return response error code:500
- 【NOIP2017提高A组模拟10.6】Biology
- [JZOJ5386]碎