LeetCode: Median of Two Sorted Arrays
2017-09-02 19:44
369 查看
https://leetcode.com/problems/median-of-two-sorted-arrays/solution/
http://www.cnblogs.com/yuzhangcmu/p/4138184.html http://blog.csdn.net/gao1440156051/article/details/51725845 http://blog.csdn.net/linhuanmars/article/details/19905515
![](https://images2017.cnblogs.com/blog/403569/201709/403569-20170902194339968-1911056065.png)
http://www.cnblogs.com/yuzhangcmu/p/4138184.html http://blog.csdn.net/gao1440156051/article/details/51725845 http://blog.csdn.net/linhuanmars/article/details/19905515
![](https://images2017.cnblogs.com/blog/403569/201709/403569-20170902194339968-1911056065.png)
class Solution { public: double findMedianSortedArrays(int A[], int m, int B[], int n) { int size = m + n; if (size & 0x1){ return findKth(A,m,B,n,size/2+1); }else{ double a = findKth(A,m,B,n,size/2); double b = findKth(A,m,B,n,size/2+1); //cout<<a<<" "<<b<<endl; return (a + b)/2; } //return mergeSort(A,m,B,n); } double findKth(int A[], int m, int B[],int n, int k){ if (m > n){ return findKth(B,n,A,m,k); } if (0 == m){ return B[k-1]; } if (1 == k){ return min(A[0],B[0]); } int pa = min(m,k/2); int 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]; } } };
相关文章推荐
- 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]Median of Two Sorted Arrays
- LeetCode之Median of Two Sorted Arrays
- [LeetCode] Median of Two Sorted Arrays
- [leetcode]Median of Two Sorted Arrays
- [Leetcode题解]004 Median of Two Sorted Arrays
- leetcode之 median of two sorted arrays
- [LeetCode][Java] 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 4---Median of Two Sorted Arrays
- leetcode——Median of Two Sorted Arrays
- [leetcode]Median of Two Sorted Arrays
- Leetcode 4. Median of Two Sorted Arrays
- Median of Two Sorted Arrays-LeetCode
- LeetCode Median of Two Sorted Arrays