# leetcode4

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

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if(nums1.empty()&&nums2.empty())
return -10.0;
int m=nums1.size();
int n=nums2.size();
vector<int> nums3(m+n);
int i=0,j=0;
while(i<m||j<n)
{
if(nums1[i]==NULL)
{
nums3[k++]=nums[j];
}
else if(nums2[j]==NULL)
{
nums3[k++]=nums[i];
}
else if(nums1[i]<nums2[j])
{
nums3[k++]=nums[i];
i++;
}
else if(nums[i]>=nums[j])
{
num3[k++]=nums[j];
j++;
}
}
int a=(m+n)%2;
if(a)
{
return (double)nums3[(m+n)/2];
}
else
{
return (double)((nums3[(m+n)/2]+nums3[(m+n)/2-1])/2);
}

}
};

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if(nums1.empty()&&nums2.empty())
return -10.0;

int m = nums1.size();
int n = nums2.size();

vector<int> nums3(m+n);

int i = 0, j = 0 , k=0;
while (i < m&&j < n)
{
if (nums1[i] <= nums2[j])
{
nums3[k++] = nums1[i];
i++;
}
else{
nums3[k++] = nums2[j];
j++;
}
}//while

while (i < m)
{
nums3[k++] = nums1[i];
i++;
}//while

while (j < n)
{
nums3[k++] = nums2[j];
j++;
}//while

int a=(m+n)%2;
if(a)
{
return (double)nums3[(m+n)/2];
}
else
{
return (double)((nums3[(m+n)/2]+nums3[(m+n)/2-1])/2.0);
}
}
};

#### LeetCode4

2017-11-07 18:19:06

#### LeetCode4—Median of Two Sorted Arrays

2017-09-05 14:50:39

#### 17_06_05 Day4 LeetCode4

2017-06-05 16:12:09

#### Leetcode4: Factorial Trailing Zeroes

2015-04-10 20:53:23

2013-02-13 09:34:06

#### 一天一道leetcode4

2017-08-09 21:34:12

#### 剑指offer(中位数-LeetCode4)

2016-08-12 10:26:45

#### leetcode4 链表的归并排序

2016-12-28 22:28:07