Leetcode 4 Median of Two Sorted Arrays
2015-03-27 15:08
429 查看
1.题目要求
There are two sorted arrays A and B 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)).
2.分析
这道题,首先需要理解中位数(Median)的定义。
举例:1,2,3的中位数是2,1,2,3,4的中位数是(2+3)/2=2.5。
弄清楚定义后,可以想出O(m+n)时间复杂度的算法,也就是两个下班分别指向A和B比较当前数A[i]和B[j],较小者的下标做自增操作,指导进行(m+n)/2次自增操作,或者某一个数组下标越界为止,代码如下:
class Solution { public: double findMedianSortedArrays(int A[], int m, int B[], int n) { int middle ; int temp = (m+n)%2; if(temp == 1) middle = (m+n)/2; else middle=(m+n)/2-1; int i=0,j=0; int count=0; int flag=0; double res; while (count<middle&&i<m&&j<n) { if(A[i]<B[j]){ i++; } else if (A[i]>B[j]) { j++; }else //当A[i]==B[j]时,两个数组下标移动需要取决于下一个数,比如{1,1}和{1,2}; //1==1因此需要i++,如果是{1,2}和{1,1},则j++ { if(i+1<m&&A[i+1]==A[i]) i++; else if(j+1<n&&B[j+1]==B[j]) j++; else i++; } count++; } if (i==m)//数组A已经越界 { if(temp==0){ res = (B[j+middle-count]+B[j+middle-count+1])/2.0; }else res = B[j+middle-count]; }else if (j==n)//数组B已经越界 { if(temp==0){ res = (A[i+middle-count]+A[i+middle-count+1])/2.0; }else res = A[i+middle-count]; }else { if (temp == 0) { if(i+1<m && A[i+1]<B[j]) res = (A[i]+A[i+1])/2.0; else if(j+1<n && B[j+1]<A[i]) res = (B[j]+B[j+1])/2.0; else res = (B[j]+A[i])/2.0; }else { res = A[i]>B[j] ? B[j]:A[i]; } } return res; } };
提交代码,呵呵,能够Accepted。但是,题目要求时O(log(m+n)),因此我们需要想其他更好的算法,重点就是抓住sorted这个字眼。看到时间复杂度O(log(m+n)),我们会立刻想到二分查找思想,因此我们就按这个思路去寻找更好的算法。代码如下:
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
- [leetcode]Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- LeetCode: 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: Median of Two Sorted Arrays
- LeetCode刷题笔录 Median Of Two Sorted Arrays
- LeetCode Online Judge 题目C# 练习 - Median of Two Sorted Arrays
- leetCode_Median of Two Sorted Arrays
- LeetCode: Median of Two Sorted Arrays
- leetcode 26: Median of Two Sorted Arrays
- LeetCode Median of Two Sorted Arrays
- LeetCode-Median of Two Sorted Arrays
- Median of Two Sorted Arrays-----LeetCode
- [LeetCode题解004]Median of Two Sorted Arrays
- LeetCode —— Median of Two Sorted Arrays
- leetcode-004:Median of Two Sorted Arrays