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

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int i = 0, j = 0, s = 0, m = nums1.size(), n = nums2.size();
bool flag = false;
int ans = 0;
int mid;
if ((m + n) & 1)flag = true, mid = (m + n) / 2 + 1;
else mid = (m + n) / 2;
while (s != mid)
{
if (i < m && j < n)
{
if (nums1[i] > nums2[j])ans = nums2[j], j++;
else if (j < n)ans = nums1[i], i++;
}
else if (i >= m && j < n)
ans = nums2[j++];
else if (i < m && j >= n)
ans = nums1[i++];
s++;
}
if (flag)return ans;
else
{
if (j >= n && i < m)
return (double)(ans + nums1[i]) / 2;
if (i >= m && j < n)
return (double)(ans + nums2[j]) / 2;
return (double)(ans + min(nums1[i], nums2[j])) / 2;
}
}
};

#### 88. Merge Sorted Array [easy] (Python)

2016-06-15 13:54:39

#### Leetcode-median of two sorted arrays

2015-05-06 22:44:18

#### leetcode-Median of Two Sorted Arrays

2014-03-17 21:26:47

#### LeetCode-Median of Two Sorted Arrays

2014-08-19 14:55:39

#### 两个有序数组的中位数Median of Two Sorted Arrays（很重要）

2016-06-21 11:11:03

#### leetcode之 median of two sorted arrays

2013-09-10 00:15:50

#### Leetcode-Median of Two Sorted Arrays

2015-09-15 21:27:53

#### Merge Two Sorted Lists -- LeetCode

2014-02-22 23:31:00

#### 【leetcode c++】21 Merge Two Sorted Lists

2015-06-18 13:37:28

#### LeetCode(40) Median of Two Sorted Arrays (两排序数组中位数)

2015-09-22 21:18:45