int findLeft(int *nums,int s,int e,int target)
{
int i=s,j=e,m=0,idx=-1;
while(i<=j)
{
m=(i+j)/2;
if(nums[m]<target) i=m+1;
else j=m-1;
if(nums[m]==target) idx=m;
}
return idx;
}
int findRight(int *nums,int s,int e,int target)
{
int i=s,j=e,m=0,idx=-1;
while(i<=j)
{
m=(i+j)/2;
if(nums[m]>target) j=m-1;
else i=m+1;
if(nums[m]==target) idx=m;
}
return idx;
}
int* searchRange(int* nums, int numsSize, int target, int* returnSize) {
*returnSize=2;
int *ret=(int *)malloc(2*sizeof(int));
ret[0]=findLeft(nums,0,numsSize-1,target);
ret[1]=findRight(nums,ret[0],numsSize-1,target);
return ret;
}
转载于:https://www.cnblogs.com/xLester/p/7570298.html