Created
March 26, 2019 04:20
-
-
Save duhruh/5fa9aabaf5ca992410e65318f99033a6 to your computer and use it in GitHub Desktop.
findMedianSortedArrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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