-
Notifications
You must be signed in to change notification settings - Fork 4
/
solution.go
42 lines (37 loc) · 888 Bytes
/
solution.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
package main
import "fmt"
//https://leetcode.com/problems/search-in-rotated-sorted-array/discuss/154836/The-INF-and-INF-method-but-with-a-better-explanation-for-dummies-like-me
func search(nums []int, target int) int {
if len(nums) == 0 {
return -1
}
left := 0
right := len(nums) - 1
for left <= right {
mid := (left + right) >> 1
var comparator int
if (target < nums[0]) && (nums[mid] < nums[0]) || (target >= nums[0]) && (nums[mid] >= nums[0]) {
comparator = nums[mid]
} else {
if target < nums[0] {
comparator = -int(^uint(0)>>1) - 1
} else {
comparator = int(^uint(0) >> 1)
}
}
if target == comparator {
return mid
}
if target > comparator {
left = mid + 1
} else if target < comparator {
right = mid - 1
}
}
return -1
}
func main() {
nums := []int{4, 5, 6, 7, 0, 1, 2}
target := 1
fmt.Println(search(nums, target))
}