Median of Two Sorted Arrays Solutions in Go
Number 4
Difficulty Hard
Acceptance 29.6%
Link LeetCode
Other languages C++
Solutions
Go solution by halfrost/LeetCode-Go
package leetcodefunc findMedianSortedArrays(nums1 []int, nums2 []int) float64 {// 假设 nums1 的长度小if len(nums1) > len(nums2) {return findMedianSortedArrays(nums2, nums1)}low, high, k, nums1Mid, nums2Mid := 0, len(nums1), (len(nums1)+len(nums2)+1)>>1, 0, 0for low <= high {// nums1: ……………… nums1[nums1Mid-1] | nums1[nums1Mid] ……………………// nums2: ……………… nums2[nums2Mid-1] | nums2[nums2Mid] ……………………nums1Mid = low + (high-low)>>1 // 分界限右侧是 mid,分界线左侧是 mid - 1nums2Mid = k - nums1Midif nums1Mid > 0 && nums1[nums1Mid-1] > nums2[nums2Mid] { // nums1 中的分界线划多了,要向左边移动high = nums1Mid - 1} else if nums1Mid != len(nums1) && nums1[nums1Mid] < nums2[nums2Mid-1] { // nums1 中的分界线划少了,要向右边移动low = nums1Mid + 1} else {// 找到合适的划分了,需要输出最终结果了// 分为奇数偶数 2 种情况break}}midLeft, midRight := 0, 0if nums1Mid == 0 {midLeft = nums2[nums2Mid-1]} else if nums2Mid == 0 {midLeft = nums1[nums1Mid-1]} else {midLeft = max(nums1[nums1Mid-1], nums2[nums2Mid-1])}if (len(nums1)+len(nums2))&1 == 1 {return float64(midLeft)}if nums1Mid == len(nums1) {midRight = nums2[nums2Mid]} else if nums2Mid == len(nums2) {midRight = nums1[nums1Mid]} else {midRight = min(nums1[nums1Mid], nums2[nums2Mid])}return float64(midLeft+midRight) / 2}func max(a int, b int) int {if a > b {return a}return b}func min(a int, b int) int {if a > b {return b}return a}