思路

归并排序

代码

func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
	nums := mergeSort(nums1, nums2)
	length := len(nums)
	if length % 2 != 0 {
		return float64(nums[(length - 1) / 2])
	}
	i := length / 2
	return (float64(nums[i]) + float64(nums[i - 1])) / 2
}


func mergeSort(nums1 []int, nums2 []int) []int {
	l1 := len(nums1)
	l2 := len(nums2)
	result := make([]int, 0, l1 + l2)

	i, j := 0, 0

	for i < l1 && j < l2 {
		if nums1[i] < nums2[j] {
			result = append(result, nums1[i])
			i++
		} else {
			result = append(result, nums2[j])
			j++
		}
	}
	result = append(result, nums1[i:]...)
	result = append(result, nums2[j:]...)
	return result
}