bool 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 true;
}
}
else if (A[beg] > A[end])
{
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 true;
}
}
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 true;
}
}
else
{
return false;
}
}
else
{
if (target == A[beg])
{
return true;
}
else
{
beg++;
end--;
}
}
}
return false;
}
[LeetCode] Search in Rotated Sorted Array II
最新推荐文章于 2021-12-18 23:10:30 发布