LeetCode#4* Median of Two Sorted Arrays
来源:互联网 发布:mac dashboard是什么 编辑:程序博客网 时间:2024/06/05 23:40
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
题意:给定两个有序序列,要求两个序列综合后的中位数。
我就是直接用插入法将它们合并成一个序列,再求中位数,该算法是O(m+n),在LeetCode上是AC的,甚至打败了一半多的人,说明其在时间复杂度的控制是不严谨的。我的代码如下:
class Solution {public: double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) { vector<int>nums; int size1=nums1.size(); int size2=nums2.size(); int size=size1+size2; int med1,med2; if(size%2==0) { med1=size/2-1; med2=size/2; } else { med1=med2=size/2; } int vis1=0,vis2=0; int i; while(vis1<size1&&vis2<size2) { while(vis1<size1&&nums1[vis1]<=nums2[vis2]){ nums.push_back(nums1[vis1]); vis1++; } if(vis1>=size1)break; nums.push_back(nums2[vis2]); vis2++; } if(vis1<size1){ for(i=vis1;i<size1;i++) nums.push_back(nums1[i]); } else if(vis2<size2) { for(i=vis2;i<size2;i++) nums.push_back(nums2[i]); } return (nums[med1]+nums[med2])/2.0; }};
后来,看了网上的算法,真正符合对数级复杂度的算法是一种通用的求两有序序列第k小元素的算法,详细介绍参考了LeetCode(4)算法参考在此,表示对该博主的感谢。
算法思想:
该方法的核心是将原问题转变成一个寻找第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小值,所以我们可以将其抛弃。
- 当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],返回其中一个。
网上的代码如下:(用递归的方式实现寻找第k小的数)
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; } };
阅读全文
0 0
- Leetcode 4 Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- Leetcode 4 Median of Two Sorted Arrays
- [leetcode 4] Median of Two Sorted Arrays
- LeetCode 4:《Median of Two Sorted Arrays》
- [Leetcode] 4 - Median of Two Sorted Arrays
- leetcode|4|Median of Two Sorted Arrays
- [Leetcode]4Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays
- LeetCode #4 Median of Two Sorted Arrays
- LeetCode-4-Median of Two Sorted Arrays
- LeetCode 4 Median of Two Sorted Arrays
- leetcode 4 Median of Two Sorted Arrays
- LeetCode 4 Median of Two Sorted Arrays
- Leetcode[4] Median of Two Sorted Arrays
- LeetCode 4 - Median of Two Sorted Arrays
- leetcode 4 -- Median of Two Sorted Arrays
- Spring AOP——java的动态代理机制详解
- html span标签 不换行 字符换行问题
- Dubbo(1)——入门基础与实例讲解
- windows消息和消息队列 二
- 算法概论习题8.8
- LeetCode#4* Median of Two Sorted Arrays
- $.ajax()方法在请求成功后老是执行error中的函数
- lintcode 字符串查找(strStr) (Java)
- maven的settings.xml配置详解
- Android 事件总线EventBus
- 关于ListView加入HeaderView之后,点击条目时获得position参数移位的BUG修正
- NOIP 2011 Senior 3
- java实现人民币数字转换成中文
- caffe+windows+vs2015 (无GPU)环境配置