Update BinarySearch.java
parent
b5059f6996
commit
feb1f5fcad
|
|
@ -90,4 +90,30 @@ class BinarySearch {
|
|||
}
|
||||
return candidate;
|
||||
}
|
||||
|
||||
public static int binarySearchLeftMost0(int[] nums, int target) {
|
||||
int i = 0, j = nums.length - 1;
|
||||
while (i <= j) {
|
||||
int m = (i + j) >>> 1;
|
||||
if (target < nums[m]) {
|
||||
j = m - 1;
|
||||
} else {
|
||||
i = m + 1;
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
public static int binarySearchRightMost0(int[] nums, int target) {
|
||||
int i = 0, j = nums.length - 1;
|
||||
while (i <= j) {
|
||||
int m = (i + j) >>> 1;
|
||||
if (target < nums[m]) {
|
||||
j = m - 1;
|
||||
} else {
|
||||
i = m + 1;
|
||||
}
|
||||
}
|
||||
return i - 1;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue