题目描述:
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。
请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。
你可以假设 nums1 和 nums2 不会同时为空。
示例 1:
nums1 = [1, 3]
nums2 = [2]
则中位数是 2.0
示例 2:
nums1 = [1, 2]
nums2 = [3, 4]
则中位数是 (2 + 3)/2 = 2.5
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/median-of-two-sorted-arrays
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:
解题代码:
版本1
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
c := mergeArrays(nums1, nums2)
cLen := len(c)
var medNum float64
if cLen%2==0{
tmpLen := cLen/2
medNum = float64(c[tmpLen]+c[tmpLen - 1])/2
}else {
medNum = float64(c[(cLen-1)/2])
}
return medNum
}
func mergeArrays(nums1, nums2 []int) []int{
if len(nums1)==0 && len(nums2)!=0{
return nums2
}
if len(nums2)==0 && len(nums1)!=0{
return nums1
}
//m := len(nums1)
//n := len(nums2)
//fmt.Printf("m len is: %v, n len is: %v\n", m, n)
c := make([]int, 0)
//
//for i := 0; i < n; i++ {
// nums1[m] = nums2[i]
// m++
//}
c = append(c, nums1...)
c = append(c, nums2...)
//fmt.Printf("c len is: %v\n", len(c))
sort.Ints(c)
return c
}
版本2
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
c := mergeArrays(nums1, nums2)
cLen := len(c)
var medNum float64
if cLen%2 == 0 {
tmpLen := cLen / 2
medNum = float64(c[tmpLen]+c[tmpLen-1]) / 2
} else {
medNum = float64(c[(cLen-1)/2])
}
return medNum
}
func mergeArrays1(nums1, nums2 []int) []int {
if len(nums1) == 0 && len(nums2) != 0 {
return nums2
}
if len(nums2) == 0 && len(nums1) != 0 {
return nums1
}
//m := len(nums1)
//n := len(nums2)
//fmt.Printf("m len is: %v, n len is: %v\n", m, n)
c := make([]int, 0)
//
//for i := 0; i < n; i++ {
// nums1[m] = nums2[i]
// m++
//}
c = append(c, nums1...)
c = append(c, nums2...)
//fmt.Printf("c len is: %v\n", len(c))
sort.Ints(c)
return c
}
func mergeArrays(nums1, nums2 []int) []int {
if len(nums1) == 0 && len(nums2) != 0 {
return nums2
}
if len(nums2) == 0 && len(nums1) != 0 {
return nums1
}
m := len(nums1)
n := len(nums2)
c := make([]int, m+n)
mIndex := 0
nIndex := 0
cIndex := 0
for mIndex < m && nIndex < n {
if nums1[mIndex] < nums2[nIndex] {
c[cIndex] = nums1[mIndex]
cIndex++
mIndex++
} else {
c[cIndex] = nums2[nIndex]
cIndex++
nIndex++
}
}
for mIndex < m {
c[cIndex] = nums1[mIndex]
cIndex++
mIndex++
}
for nIndex < n {
c[cIndex] = nums2[nIndex]
cIndex++
nIndex++
}
return c
}
版本3
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
c := mergeArrays(nums1, nums2)
cLen := len(c)
var medNum float64
if cLen%2 == 0 {
tmpLen := cLen / 2
medNum = float64(c[tmpLen]+c[tmpLen-1]) / 2
} else {
medNum = float64(c[(cLen-1)/2])
}
return medNum
}
func mergeArrays1(nums1, nums2 []int) []int {
if len(nums1) == 0 && len(nums2) != 0 {
return nums2
}
if len(nums2) == 0 && len(nums1) != 0 {
return nums1
}
//m := len(nums1)
//n := len(nums2)
//fmt.Printf("m len is: %v, n len is: %v\n", m, n)
c := make([]int, 0)
//
//for i := 0; i < n; i++ {
// nums1[m] = nums2[i]
// m++
//}
c = append(c, nums1...)
c = append(c, nums2...)
//fmt.Printf("c len is: %v\n", len(c))
sort.Ints(c)
return c
}
func mergeArrays(nums1, nums2 []int) []int {
if len(nums1) == 0 && len(nums2) != 0 {
return nums2
}
if len(nums2) == 0 && len(nums1) != 0 {
return nums1
}
m := len(nums1)
n := len(nums2)
c := make([]int, m+n)
mIndex := 0
nIndex := 0
cIndex := 0
for mIndex < m && nIndex < n {
if nums1[mIndex] < nums2[nIndex] {
c[cIndex] = nums1[mIndex]
cIndex++
mIndex++
} else {
c[cIndex] = nums2[nIndex]
cIndex++
nIndex++
}
}
for mIndex < m {
c[cIndex] = nums1[mIndex]
cIndex++
mIndex++
}
for nIndex < n {
c[cIndex] = nums2[nIndex]
cIndex++
nIndex++
}
return c
}