Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?Would this affect the run-time complexity? How and why?
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
Write a function to determine if a given target is in the array.
The array may contain duplicates.
比起之前的题多了重复数字,需要做的是,如果相同 则把左边界前移
public class Solution {
public boolean search(int[] nums, int target) {
if(nums.length==0)return false;
int l = 0;
int r = nums.length-1;
while(l<=r){
int m = (l+r)/2;
if(nums[m]==target)return true;
if(nums[m]>nums[l]){
if(nums[m]>target&&nums[l]<=target){
r = m-1;
}
else l = m+1;
}
else if(nums[m]<nums[l]){
if(nums[m]<target&&nums[r]>=target){
l = m+1;
}
else r = m-1;
}
else l++;
}
return false;
}
}