Skip to content

Commit c46b45c

Browse files
committed
Q35 - Search Insert Position solution added
1 parent a103eff commit c46b45c

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package src.common;
2+
3+
public class Q35_SearchInsertPosition {
4+
public static void main(String[] args) {
5+
int nums[] = {1,3,5,7,9};
6+
System.out.println(searchInsert(nums,4));
7+
}
8+
public static int searchInsert(int[] nums, int target) {
9+
int start = 0;
10+
int end = nums.length-1;
11+
12+
while (start <= end) {
13+
int mid = start + (end-start)/2;
14+
if (nums[mid] == target) return mid;
15+
else if (nums[mid] > target) end = mid-1;
16+
else start = mid+1;
17+
}
18+
19+
return start;
20+
}
21+
}

0 commit comments

Comments
 (0)