Leetcode 81 Search in Rotated Sorted Array II
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
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?
sorted array 应该分为2个上升部分。
使用二分法查找,因为存在duplicates, 所以可能会出现 nums[end] == nums[start] 或 nums[end] == nums[mid] == nums[start] 的情况。
基于leetcode 33 one pass way binary search的改进
例子:
input : [1, 1, 3, 1] and search 3
- [1 start, 1 mid, 3, 1 end] start++
- [1, 1 start, 3 mid, 1 end] return true