二分法:
public class Solution {
public int[] searchRange(int[] A, int target) {return msearch(A,target,0,A.length-1);
}
public static int[] msearch(int[] A, int target, int begin, int end) {
int mid,i;
int[] rst={-1,-1};
int n=end-begin+1;
if(n==0)
return rst;
if(n==1)
{
if(A[begin]==target)
{
rst[0]=begin;rst[1]=begin;
return rst;
}
else
return rst;
}
mid=begin+n/2;
if(target>A[mid])
{
return msearch(A,target,mid+1,end);
}
else if(target<A[mid])
{
return msearch(A,target,begin,mid-1);
}
else
{
i=mid;
while(i>=begin && A[i]==target)
{
i--;
}
rst[0]=i+1;
i=mid;
while(i<=end && A[i]==target)
{
i++;
}
rst[1]=i-1;
}
return rst;
}
}