forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.go
48 lines (46 loc) · 961 Bytes
/
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
/**
* @lc app=leetcode.cn id=34 lang=golang
* Accepted
* 88/88 cases passed (12 ms)
* Your runtime beats 90.75 % of golang submissions
* Your memory usage beats 60.76 % of golang submissions (4.1 MB)
*/
func searchRange(nums []int, target int) []int {
l, m, r := 0, 0, len(nums)-1
for l <= r {
m = (l + r) / 2
if nums[m] == target {
return []int{findFirst(nums, l, m, target), findEnd(nums, m, r, target)}
}
if nums[m] < target {
l = m + 1
} else if nums[m] > target {
r = m - 1
}
}
return []int{-1, -1}
}
func findFirst(nums []int, l, r, target int) int {
var m int
for l < r {
m = (l + r) / 2
if nums[m] < target {
l = m + 1
} else if nums[m] == target {
r = m
}
}
return l
}
func findEnd(nums []int, l, r, target int) int {
var m int
for l < r {
m = (l + r + 1) / 2 // 需要加一,否则陷入死循环
if nums[m] > target {
r = m - 1
} else if nums[m] == target {
l = m
}
}
return l
}