class Solution {
public int[] searchRange(int[] nums, int target) {
int[] a = new int[2];
a[0] = binarySearch1(nums,target);
a[1] = binarySearch2(nums,target);
return a;
}
public int binarySearch1(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
int leftI = -1;
while(left <= right){
int mid = (left + right) / 2;
if (nums[mid] == target){
leftI = mid;
right = mid - 1;
}
if (nums[mid] > target){
right = mid - 1;
}
if (nums[mid] < target){
left = mid + 1;
}
}
return leftI;
}
public int binarySearch2(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
int rightI = -1;
while(left <= right){
int mid = (left + right) / 2;
if (nums[mid] == target){
rightI = mid;
left = mid + 1;
}
if (nums[mid] > target){
right = mid - 1;
}
if (nums[mid] < target){
left = mid + 1;
}
}
return rightI;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47