Skip to content

Instantly share code, notes, and snippets.

@duhruh
Created March 26, 2019 04:20
Show Gist options
  • Save duhruh/5fa9aabaf5ca992410e65318f99033a6 to your computer and use it in GitHub Desktop.
Save duhruh/5fa9aabaf5ca992410e65318f99033a6 to your computer and use it in GitHub Desktop.
findMedianSortedArrays
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
i := 0
j := 0
combined := []int{}
for {
if i == len(nums1) || j == len(nums2) {
break
}
if nums1[i] < nums2[j] {
combined = append(combined, nums1[i])
i++
} else if nums1[i] > nums2[j] {
combined = append(combined, nums2[j])
j++
} else {
combined = append(combined, nums1[i])
combined = append(combined, nums2[j])
i++
j++
}
}
if i == len(nums1) {
for j = j; j < len(nums2); j++ {
combined = append(combined, nums2[j])
}
} else {
for i = i; i < len(nums1); i++ {
combined = append(combined, nums1[i])
}
}
if len(combined)%2 != 0 {
middle := len(combined) / 2
return float64(combined[middle])
}
middle := len(combined) / 2
return float64(combined[middle-1]+combined[middle]) / 2.0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment