# 【题解】【数组】【Leetcode】Median of Two Sorted Arrays

#### Median of Two Sorted Arrays

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)).

?

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 `double` `findMedianSortedArrays(``int` `A[], ``int` `m, ``int` `B[], ``int` `n) {` `    ``if``(m<3 || n<3) ` `        ``return` `TrivialMedian(A,m,B,n);` `    ``int` `i = m/2;` `    ``int` `j = (m % 2 == 0 && n % 2 == 0) ? n/2-1 : n/2;` `    ``if``(A[i] >= B[j]){` `        ``if``(j >= m-i-1) ` `            ``return` `findMedianSortedArrays(A,i+1,B+m-i-1,n-m+i+1);` `        ``else` `            ``return` `findMedianSortedArrays(A,m-j,B+j,n-j);` `    ``}``else``{` `        ``if``(i <= n-j-1)` `            ``return` `findMedianSortedArrays(A+i,m-i,B,n-i);` `        ``else` `            ``return` `findMedianSortedArrays(A+n-j-1,m-n+j+1,B,j+1);` `    ``}` `}` `double` `TrivialMedian(``int` `A[], ``int` `m, ``int` `B[], ``int` `n){` `    ``vector<``int``> nums ;` `    ``for``(``int` `i = 0; i < m; i++)` `        ``nums.push_back(A[i]);` `    ``for``(``int` `i = 0; i < n; i++)` `        ``nums.push_back(B[i]);` `    ``sort(nums.begin(),nums.end());` `    ``int` `k = m+n;` `    ``if``(k % 2 == 0)` `        ``return` `0.5*(nums[k/2]+nums[k/2 - 1]);` `    ``else` `        ``return` `nums[k/2];` `}`

【题解】【数组】【Leetcode】Median of Two Sorted Arrays,布布扣,bubuko.com

## LeetCode——Median of Two Sorted Arrays

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)). 有两个已排序的数组A和B,大小为m 和 n. 找出两数组的中位数 时间复杂度应该为 O(log (m+n)). 简单粗暴的方法就是把两个数组合并成一个数组,排序,取中位数.

## LeetCode—Median of Two Sorted Arrays

Median of Two Sorted Arrays 这道题要找Median,中位数.这个是指,如果数组大小是偶数,返回中间两个数的平均值,如果是奇数个,就是中间的数. 算法时间效率要求是 O(log(m + n)),具体思路网上都一样. 另外,现在leetCode的C++ 数组都换成vector了,所以只好整理一下vector的用法. 参考:http://imatlab.lofter.com/post/286ffc_a81276 http://www.cnblogs.com/wang7/ar

## [JS做LeetCode] Median of Two Sorted Arrays

Median of Two Sorted Arrays <html> <head> <meta http-equiv="charset" content="utf-8"></head> <body> <script> //解题思路: //找出中位数要循环的次数为loop //posA为数组A的下标,posB为数组B的下标 //每次循环找出两个数组对应下标比较小的值,进栈,下标+1 //注意判断数

## [LeetCode] Median of Two Sorted Arrays 两个有序数组的中位数

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)). http://www.acmerblog.com/leetcode-median-of-two-sorted-arrays-5330.html http:/

## [LeetCode] Median of Two Sorted Arrays 解题报告

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)). Subscribe to see which companies asked this question Show Tags 分析: 这个题目在leetco