-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsearchRange.go
More file actions
36 lines (35 loc) · 767 Bytes
/
searchRange.go
File metadata and controls
36 lines (35 loc) · 767 Bytes
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
package searchforarange
func searchRange(nums []int, target int) []int {
result := []int{-1, -1}
if len(nums) == 1 {
if nums[0] == target {
return []int{0, 0}
}
} else if len(nums) >= 2 {
low, high := 0, len(nums)-1
// Search for the left one using binary search.
for low < high {
middle := (low + high) / 2
if nums[middle] < target {
low = middle + 1
} else {
high = middle
}
}
if nums[low] == target {
result[0] = low
high = len(nums) - 1
// Search for the right one using binary search.
for low < high {
middle := (low+high)/2 + 1 // middle value trends to the right
if nums[middle] > target {
high = middle - 1
} else {
low = middle
}
}
result[1] = high
}
}
return result
}