-
Notifications
You must be signed in to change notification settings - Fork 0
/
33-search.go
46 lines (42 loc) · 1.01 KB
/
33-search.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
// https://leetcode.cn/problems/search-in-rotated-sorted-array/
package main
import "fmt"
/**
* 二分后,两个子数组必有一个是有序的
* 如果在有序数组范围内,则二分查找
* 如果不在有序数组,继续二分
* 迭代前两步
* 时间: O(log(N)
* 空间: O(1)
*/
func search(nums []int, target int) int {
l, r := 0, len(nums)-1
for l <= r {
mid := (l + r) / 2
if target == nums[mid] {
return mid
}
if nums[l] <= nums[mid] {
if nums[l] <= target && target < nums[mid] {
r = mid - 1
} else {
l = mid + 1
}
} else {
if nums[mid] < target && target <= nums[r] {
l = mid + 1
} else {
r = mid - 1
}
}
}
return -1
}
func main() {
numss := [][]int{{4, 5, 6, 7, 0, 1, 2}, {4, 5, 6, 7, 0, 1, 2}, {1}, {5, 1, 3}, {3, 1}}
targets := []int{0, 3, 0, 5, 1}
answers := []int{4, -1, -1, 0, 1}
for i := 0; i < len(numss) && i < len(targets); i++ {
fmt.Println(numss[i], targets[i], "ans:", answers[i], "ret:", search(numss[i], targets[i]))
}
}