Search for a range
public int[] searchRange(int[] nums, int target) {
if ( nums == null || nums.length == 0 )
return new int[]{-1, -1};
// first position
int start = 0 ;
int end = nums.length - 1;
int[] bound = new int[2];
while ( start + 1 < end ){
int mid = start + (end - start) / 2;
if ( nums[mid] < target){
start = mid;
}else {
end = mid;
}
}
if ( nums[start] == target) bound[0] = start;
else if (nums[end] == target) bound[0] = end;
else return new int[]{-1, -1};
//last position
start = 0 ;
end = nums.length - 1;
while ( start + 1 < end ){
int mid = start + (end - start) / 2;
if ( nums[mid] > target){
end = mid;
}else {
start = mid;
}
}
if ( nums[end] == target) bound[1] = end;
else if (nums[start] == target) bound[1] = start;
else return new int[]{-1, -1};
return bound;
}