owned this note
owned this note
Published
Linked with GitHub
# 4. Median of Two Sorted Arrays
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).
- Example 1:
```
Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.
```
- Example 2:
```
Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
```
- Example 3:
```
Input: nums1 = [0,0], nums2 = [0,0]
Output: 0.00000
```
- Example 4:
```
Input: nums1 = [], nums2 = [1]
Output: 1.00000
```
- Example 5:
```
Input: nums1 = [2], nums2 = []
Output: 2.00000
```
- Constraints:
```
nums1.length == m
nums2.length == n
0 <= m <= 1000
0 <= n <= 1000
1 <= m + n <= 2000
-106 <= nums1[i], nums2[i] <= 106
```
## C
* 第4,5行在處理Example 4,5的case
* current_num1 代表 nums1 的 ptr
* current_num2 代表 nums2 的 ptr
* count 代表 merged 的 array 的 ptr

* flag1 和 flag2 來判斷陣列是否已經為空
```C=
#include <stdlib.h>
#include <stdio.h>
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) {
int count = nums1Size + nums2Size;
if (nums2Size == 0 && nums1Size == 1) return (double)nums1[0];
if (nums1Size == 0 && nums2Size == 1) return (double)nums2[0];
int* array = (int*)malloc(sizeof(int) * count);
int num1, num2;
int current_num1 = 0;
int current_num2 = 0;
int total = 0;
int flag1 = 0;
int flag2 = 0;
for (int i = 0; i < count; i++) {
if (nums1Size == 0) {
num1 = 0;
flag1 = 1;
}
else {
num1 = nums1[current_num1];
flag1 = 0;
}
if (nums2Size == 0) {
num2 = 0;
flag2 = 1;
}
else {
num2 = nums2[current_num2];
flag2 = 0;
}
if (flag1 == 0 && flag2 == 0) {
if (num1 <= num2) {
total += num1;
array[i] = num1;
current_num1++;
if (nums1Size != 0)nums1Size--;
}
else {
total += num2;
array[i] = num2;
current_num2++;
if (nums2Size != 0)nums2Size--;
}
}
else if(flag1 == 0 && flag2 == 1){
total += num1;
array[i] = num1;
current_num1++;
if (nums1Size != 0)nums1Size--;
}
else if (flag1 == 1 && flag2 == 0) {
total += num2;
array[i] = num2;
current_num2++;
if (nums2Size != 0)nums2Size--;
}
else {//flag1 == 1 && flag2 == 1
}
}
if ((count % 2) != 0) return array[(count / 2)];
else return ((double)(array[(count / 2)] + array[(count / 2) - 1])) / 2;
}
void main() {
int nums1Size = 2;
int* nums1 = (int*)malloc(sizeof(int) * nums1Size);
int nums2Size = 2;
int* nums2 = (int*)malloc(sizeof(int) * nums2Size);
nums1[0] = 1;
nums1[1] = 2;
nums2[0] = 3;
nums2[1] = 4;
printf("%f\n",findMedianSortedArrays(nums1, nums1Size, nums2, nums2Size));
}
```
## C++