add new solutions
This commit is contained in:
parent
d798d5e8c9
commit
886b5e0a8e
34 changed files with 1164 additions and 0 deletions
26
solutions/0/q34/solution.go
Normal file
26
solutions/0/q34/solution.go
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
package q34
|
||||
|
||||
func find(nums []int, target int) int {
|
||||
l, r := 0, len(nums)
|
||||
for l < r {
|
||||
m := (l + r) / 2
|
||||
switch {
|
||||
case nums[m] < target:
|
||||
l = m + 1
|
||||
default:
|
||||
r = m
|
||||
}
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func searchRange(nums []int, target int) []int {
|
||||
pos := find(nums, target)
|
||||
if pos < 0 || pos >= len(nums) || nums[pos] != target {
|
||||
return []int{-1, -1}
|
||||
}
|
||||
pos2 := find(nums[pos:], target+1)
|
||||
return []int{pos, pos + pos2 - 1}
|
||||
}
|
||||
|
||||
var _ = searchRange
|
||||
Loading…
Add table
Add a link
Reference in a new issue