版权声明:原创勿转 https://cloud.tencent.com/developer/article/1419695
思路
简单的二分查找
code
代码语言:javascript复制func search(nums []int, target int) int {
if len(nums) == 0 {
return -1
}
start, end := 0, len(nums)-1
for start <= end {
mid := (start end) / 2
if nums[mid] == target {
return mid
}
if nums[mid] > target {
end = mid - 1
continue
}
if nums[mid] < target {
start = mid 1
}
}
return -1
}