Skip to content

Commit f3a2e5d

Browse files
committed
add: FindTheInsertionIndex
1 parent 23eb148 commit f3a2e5d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class FindTheInsertionIndex {
2+
public int findTheInsertionIndex(int[] nums, int target) {
3+
int left = 0;
4+
int right = nums.length;
5+
while (left < right) {
6+
int mid = (left + right) / 2;
7+
// If the midpoint value is greater than or equal to the target,
8+
// the lower bound is either at the midpoint, or to its left.
9+
if (nums[mid] >= target) {
10+
right = mid;
11+
}
12+
// The midpoint value is less than the target, indicating the
13+
// lower bound is somewhere to the right.
14+
else {
15+
left = mid + 1;
16+
}
17+
}
18+
return left;
19+
}
20+
}

0 commit comments

Comments
 (0)