-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
47 lines (45 loc) · 858 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package _0004
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
nums := merge(nums1, nums2)
if len(nums) == 0 {
return 0
}
if len(nums) == 1 {
return float64(nums[0])
}
mid := len(nums) / 2
if len(nums)%2 == 0 {
return float64(nums[mid]+nums[mid-1]) / 2
}
return float64(nums[mid])
}
func merge(nums1 []int, nums2 []int) []int {
if len(nums1) == 0 {
return nums2
}
if len(nums2) == 0 {
return nums1
}
m, n := 0, 0
tmp := make([]int, 0)
for m < len(nums1) && n < len(nums2) {
if nums1[m] < nums2[n] {
tmp = append(tmp, nums1[m])
m++
} else if nums1[m] == nums2[n] {
tmp = append(tmp, nums1[m], nums2[n])
m++
n++
} else {
tmp = append(tmp, nums2[n])
n++
}
}
if m < len(nums1) {
tmp = append(tmp, nums1[m:]...)
}
if n < len(nums2) {
tmp = append(tmp, nums2[n:]...)
}
return tmp
}