public class Solution {
public int[] searchRange(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return new int[] {-1, -1};
}
int leftBound = -1, rightBound = -1;
// target leftBound
int left = 0, right = nums.length - 1;
int mid;
while (left + 1 < right) {
mid = left + (right - left) / 2;
if (target <= nums[mid]) {
right = mid;
} else {
left = mid;
}
}
if (target == nums[right]) {
leftBound = right;
}
if (target == nums[left]) {
leftBound = left;
}
// target rightBound
left = 0;
right = nums.length - 1;
while (left + 1 < right) {
mid = left + (right - left) / 2;
if (target < nums[mid]) {
right = mid;
} else {
left = mid;
}
}
if (target == nums[left]) {
rightBound = left;
}
if (target == nums[right]) {
rightBound = right;
}
return new int[] {leftBound, rightBound};
}
}