Golang Leetcode 287. Find the Duplicate Number.go

2019-04-12 13:55:36 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412945

思路

二分查找

code

代码语言:javascript复制
func findDuplicate(nums []int) int {
	left, right := 1, len(nums)-1
	mid := left   (right-left)/2
	for left < right {
		c := 0
		mid = left   (right-left)/2
		for i := 0; i < len(nums); i   {
			if nums[i] <= mid {
				c  
			}
		}
		if c > mid {
			right = mid
		} else {
			left = mid   1
		}
	}
	return left
}

0 人点赞