题解 | #在旋转过的有序数组中寻找目标值#
在旋转过的有序数组中寻找目标值
https://www.nowcoder.com/practice/87c0e7abcbda41e7963660fa7d020995
package main /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型 */ func search(nums []int, target int) int { l := 0 r := len(nums) - 1 for l <= r { mid := (l + r) / 2 if mid == 0 { if mid+1 < len(nums) && nums[mid] < nums[mid+1] { r = mid - 1 } else { l = mid + 1 } } else { if nums[mid]-nums[0] > 0 { l = mid + 1 } else { r = mid - 1 } } } ll := 0 rr := l - 1 for ll <= rr { mid := (ll + rr) / 2 if nums[mid] == target { return mid } else if nums[mid] < target { ll = mid + 1 } else { rr = mid - 1 } } ll = l rr = len(nums) - 1 for ll <= rr { mid := (ll + rr) / 2 if nums[mid] == target { return mid } else if nums[mid] < target { ll = mid + 1 } else { rr = mid - 1 } } return -1 // write code here }