website/content.en/ChapterFour/0001~0099/0081.Search-in-Rotated-Sorted-Array-II.md
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2]).
You are given a target value to search. If found in the array return true, otherwise return false.
Example 1:
Input: nums = [2,5,6,0,0,1,2], target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2], target = 3
Output: false
Follow up:
nums may contain duplicates.Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (For example, the array [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2] ).
Write a function to determine whether a given target value exists in the array. If it exists, return true; otherwise, return false.
Follow up:
true; if not found, output false.true and false. For the detailed approach, see Problem 33.
package leetcode
func search(nums []int, target int) bool {
if len(nums) == 0 {
return false
}
low, high := 0, len(nums)-1
for low <= high {
mid := low + (high-low)>>1
if nums[mid] == target {
return true
} else if nums[mid] > nums[low] { // in the interval with larger values
if nums[low] <= target && target < nums[mid] {
high = mid - 1
} else {
low = mid + 1
}
} else if nums[mid] < nums[high] { // in the interval with smaller values
if nums[mid] < target && target <= nums[high] {
low = mid + 1
} else {
high = mid - 1
}
} else {
if nums[low] == nums[mid] {
low++
}
if nums[high] == nums[mid] {
high--
}
}
}
return false
}