标签:style blog http color io os ar for sp
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.
int search(int A[], int n, int target){
int start = 0, end = n, middle ;
while(start < end){
middle = (start + end) / 2;
if(A[middle] == target) return middle;
if(A[middle] > A[start]){
if(target >= A[start] && target < A[middle]){
end = middle;
}else{
start = middle + 1;
}
}else if(A[middle] < A[start]){
if(target > A[middle] && target <= A[end - 1]){
start = middle + 1;
}else{
end = middle;
}
}
else{
++start;
}
}
return -1;
}
leetcode 二分查找 Search in Rotated Sorted ArrayII
标签:style blog http color io os ar for sp
原文地址:http://blog.csdn.net/zhengsenlie/article/details/39968259