Problem
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:
1 |
|
Example 2:
1 |
|
Follow up:
- This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates.
- Would this affect the run-time complexity? How and why?
Explanation
- Similar to 33. Search in Rotated Sorted Array, now, we also need to consider the case that
nums[mid] == nums[left] == num[right]
. In this case, we can moveleft
one step forward, so themid
will also move forward, until!(nums[mid] == nums[left] == num[right])
, and now it is the same as 33. Search in Rotated Sorted Array.
Solution
1 |
|