4. 寻找两个有序数组的中位数

题目描述:

给定两个大小为 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
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值