LeetCode 面试题 10.03. 搜索旋转数组
题目描述
思路分析
思路描述
参考代码
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
func search(nums []int, target int) int {
left, right := 0, len(nums)-1
res := -1
for left <= right {
if nums[left] == target {
return left
}
mid := left + (right-left)/2
if nums[mid] == target {
res = mid
right = mid - 1
} else if nums[mid] > nums[right] {
if nums[left] <= target && target < nums[mid] {
right = mid - 1
} else {
left = mid + 1
}
} else if nums[mid] < nums[right] {
if nums[mid] < target && target <= nums[right] {
left = mid + 1
} else {
right = mid - 1
}
} else {
right--
}
}
return res
}
1
write your code here
CC BY-NC-SA 4.0
许可协议,转载请注明出处!
本博客所有文章除特别声明外,均采用