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
| func searchRange(nums []int, target int) []int { if len(nums) == 0 { return []int{-1, -1} }
ans := []int{} left, right := 0, len(nums) - 1 for left < right { mid := (left + right) / 2 if nums[mid] >= target { right = mid } else { left = mid + 1 } }
if nums[right] != target { ans = []int{-1, -1} return ans } else { ans = append(ans, right) }
right = len(nums) - 1 for left < right { mid := (left + right + 1) / 2 if nums[mid] <= target { left = mid } else { right = mid - 1 } }
if nums[right] == target { ans = append(ans, right) } else { ans = append(ans, ans[0]) }
return ans
}
|