int search(int A[], int n, int target) {
int beg = 0, end = n-1;
while(beg <= end)
{
int mid = (beg+end)/2;
if(A[beg] <= A[end])
{
if(target < A[mid])
{
end = mid - 1;
}
else if(target > A[mid])
{
beg = mid + 1;
}
else
{
return mid;
}
}
else
{
if(target > A[beg])
{
if(A[mid] < A[beg] || A[mid] > target)
{
end = mid - 1;
}
else if(A[mid] < target)
{
beg = mid + 1;
}
else
{
return mid;
}
}
else if(target == A[beg])
{
return beg;
}
else if(target < A[end])
{
if(A[mid] > A[end] || A[mid] < target)
{
beg = mid + 1;
}
else if(A[mid] > target)
{
end = mid - 1;
}
else
{
return mid;
}
}
else if(target == A[end])
{
return end;
}
else
{
return -1;
}
}
}
return -1;
}
[LeetCode] Search in Rotated Sorted Array
最新推荐文章于 2021-12-18 23:10:30 发布