Update BinarySearch.java

master
wangsiyuan 2024-07-10 17:11:18 +08:00
parent b5059f6996
commit feb1f5fcad
1 changed files with 26 additions and 0 deletions

View File

@ -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;
}
}