leetcode_Search in Rotated Sorted Array II

时间:2022-08-02 11:36:01
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array.

  

class Solution {
public:
bool search(int A[], int n, int target) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(A == NULL || n <) return false; int left = ;
int right = n-;
while(left <= right){ int mid = (right - left)/ + left;
if(A[mid] == target)
return true;
if(A[mid] > A[left])
{
if(target >= A[left] && target < A[mid])
right = mid - ;
else
left = mid + ;
}else if(A[mid] < A[left]){ if(target <= A[right] && target > A[mid])
left = mid + ;
else
right = mid -;
}else{
if(A[left] == target)
return true; ++left ;
}
}
return false;
}
};