leetcode-go/code-master/array/704_binrary_search/main.go

32 lines
395 B
Go

package main
func search(nums []int, target int) int {
// 如果沒有傳入就先回傳了
if len(nums) == 0 {
return -1
}
left, right := 0, len(nums)-1
for right >= left {
mid := left + (right-left)>>1
if nums[mid] > target {
right = mid - 1
}
if nums[mid] < target {
left = mid + 1
}
if nums[mid] == target {
return mid
}
}
return -1
}
func main() {
}