版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412873
思路
二分查找,注意到每个点的时候,都要判断方向
code
代码语言:javascript复制func search(nums []int, target int) bool {
l := len(nums)
if l == 0 {
return false
}
left, right := 0, l-1
for left <= right {
mid := left (right-left)/2
if nums[mid] == target {
return true
} else if nums[mid] < nums[right] {
if nums[mid] < target && nums[right] >= target {
left = mid 1
} else {
right = mid - 1
}
} else if nums[mid] > nums[right] {
if nums[left] <= target && nums[mid] > target {
right = mid - 1
} else {
left = mid 1
}
} else {
right--
}
}
return false
}