-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathleetcode0081.go
60 lines (49 loc) · 1.12 KB
/
leetcode0081.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
*/
package leetcode
// NOTE: The name should be search. Rename it to avoid conflicts with leetcode0033.go
func search81(nums []int, target int) bool {
if len(nums) == 0 {
return false
}
binarySearch := func(nums []int, start, end, target int) bool {
for start <= end {
mid := (start + end) / 2
if nums[mid] == target {
return true
}
if nums[mid] < target {
start = mid + 1
} else {
end = mid - 1
}
}
return false
}
left, right, smallest := 0, len(nums)-1, 0
for left < right && nums[left] >= nums[right] {
if right == left+1 {
smallest = right
break
}
mid := (left + right) / 2
if nums[left] == nums[right] && nums[left] == nums[mid] {
smallest, left, right = right, left+1, right-1
continue
}
if nums[mid] >= nums[left] && nums[mid] >= nums[right] {
left = mid
} else {
right = mid
}
}
if target >= nums[0] {
end := smallest - 1
if smallest == 0 {
end = len(nums) - 1
}
return binarySearch(nums, 0, end, target)
}
return binarySearch(nums, smallest, len(nums)-1, target)
}