题目:
解答:
上一题的解法一代码可以直接通过。
Would this affect the run-time complexity? How and why?
如果采用解法一方法的话不会,采用其他方法的话可能会。
代码:
class Solution {
public:
bool search(int A[], int n, int target) {
int t = 0;
for (int i = 0; i < n - 1; i++)
{
if (A[i] > A[i + 1])
{
t = (i + 1) % n;
break;
}
}
int startReal = 0;
int endReal = n-1;
int mid, midReal;
while (startReal <= endReal)
{
midReal = startReal + (endReal - startReal) / 2;
mid = (midReal + t + n) % n;
if (A[mid] == target)
return true;
if (A[mid] < target)
{
startReal = midReal + 1;
}
else
{
endReal = midReal - 1;
}
}
return false;
}
};