-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathSolution.go
68 lines (63 loc) · 1.16 KB
/
Solution.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* Report by leetcode.com
* Runtime: 16 ms, Memory Usage: 5.4 MB
*/
func findMedianSortedArrays(nums1 []int, nums2 []int) float64 {
mathMax := func(a, b int) int {
if a > b {
return a
}
return b
}
mathMin := func(a, b int) int {
if a < b {
return a
}
return b
}
var len1, len2 int
len1 = len(nums1)
len2 = len(nums2)
if len1 > len2 {
var tmp []int = nums1
nums1 = nums2
nums2 = tmp
var t int = len1
len1 = len2
len2 = t
}
var min, max int = 0, 0
max = len1
halfLen := (len1 + len2 + 1) / 2
for min <= max {
i := (min + max) / 2
j := halfLen - i
if i < max && nums2[j-1] > nums1[i] {
min++
} else if i > min && nums1[i-1] > nums2[j] {
max--
} else {
var maxLeft int
if i == 0 {
maxLeft = nums2[j-1]
} else if j == 0 {
maxLeft = nums1[i-1]
} else {
maxLeft = mathMax(nums1[i-1], nums2[j-1])
}
if ((len1 + len2) & 1) == 1 {
return float64(maxLeft)
}
var minRight int
if i == len1 {
minRight = nums2[j]
} else if j == len2 {
minRight = nums1[i]
} else {
minRight = mathMin(nums2[j], nums1[i])
}
return float64(maxLeft+minRight) / 2
}
}
return 0
}