Golang Leetcode 704. Binary Search.go

2019-04-29 18:05:39 浏览数 (1)

版权声明:原创勿转 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
}

0 人点赞