Skip to content

Commit 94b30f0

Browse files
committed
commit
1 parent 9d405a5 commit 94b30f0

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

35. Search Insert Position.java

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int searchInsert(int[] nums, int target) {
3+
// Binary search algorithm
4+
int left = 0, right = nums.length - 1;
5+
6+
while (left <= right) {
7+
int mid = (left + right) / 2;
8+
9+
// Base case
10+
if (nums[mid] == target) return mid;
11+
// Shrink boundary
12+
else if (nums[mid] > target) right = mid - 1;
13+
else left = mid + 1;
14+
}
15+
16+
return left;
17+
}
18+
}

0 commit comments

Comments
 (0)