# C#LeetCode刷题之#4-两个排序数组的中位数（Median of Two Sorted Arrays）

nums1 = [1, 3]

nums2 = [2]

`nums1 = [1, 2]`

`nums2 = [3, 4]`

`中位数是 (2 + 3)/2 = 2.5`

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

You may assume nums1 and nums2 cannot be both empty.

`nums1 = [1, 3]`

`nums2 = [2]`

`The median is 2.0`

`nums1 = [1, 2]`

`nums2 = [3, 4]`

`The median is (2 + 3)/2 = 2.5`

```public class Program {

public static void Main(string[] args) {
int[] nums1 = { 1, 2, 3 };
int[] nums2 = { 4, 5 };

var res = FindMedianSortedArrays(nums1, nums2);

Console.WriteLine(\$"The median is {res}.");

}

private static double FindMedianSortedArrays(int[] nums1, int[] nums2) {
int size = nums1.Length + nums2.Length;

int[] union = new int[size];

int mid = size / 2;
int even = (size % 2 == 0) ? 1 : 0;

int m1, m2;
int left, right;

for(int i = m1 = m2 = 0; i < size; i++) {
left = m1 < nums1.Length ? nums1[m1] : int.MaxValue;
right = m2 < nums2.Length ? nums2[m2] : int.MaxValue;

if(left < right) {
union[m1 + m2] = nums1[m1];
m1++;
} else {
union[m1 + m2] = nums2[m2];
m2++;
}

if(i == mid) break;
}
return (union[mid] + union[mid - even]) / 2.0;
}

}```

`The median is 3.`

(1)