s,e,m分别是二分起,终,中
根据A[s]和A[m]关系确定s与m间是否发生了旋转,结合A[s]与target关系确定目标应在哪个区间
-
class Solution {
-
public:
-
int search(int A[], int n, int target) {
-
int s=0;
-
int e=n-1;
-
int m=(s+e)/2;
-
-
while(s<=e)
-
{
-
m=(s+e)/2;
-
if(A[m]==target) return m;
-
else if(A[m]<target)
-
{
-
if(A[s]<=A[m])
-
{
-
s=m+1;
-
}
-
else
-
{
-
if(A[s]>target) s=m+1;
-
else if(A[s]<target) e=m-1;
-
else return s;
-
}
-
-
}
-
else if(A[m]>target)
-
{
-
if(A[s]<=A[m])
-
{
-
if(A[s]<target) e=m-1;
-
else if(A[s]>target) s=m+1;
-
else return s;
-
}
-
else
-
{
-
e=m-1;
-
}
-
}
-
}
-
return -1;
-
}
-
};
阅读(103) | 评论(0) | 转发(0) |