Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return[-1, -1]
For example,Given[5, 7, 7, 8, 8, 10]
and target value 8,return[3, 4]
public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] range = {-1,-1};
int start = 0,end = nums.length-1;
int mid = (start+end)>>1;
while(start<=end)
{
mid = (start+end)>>1;
if(target==nums[mid])
break;
else if(target>nums[mid])
start = mid+1;
else
end = mid-1;
}
if(start>end)
return range;
int left = mid,right = mid;
while(left>0&&nums[left-1]==nums[mid])
left--;
while(right<nums.length-1&&nums[right+1]==nums[mid])
right++;
range[0] = left;
range[1] = right;
return range;
}
}