-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearchInsertPosition.java
More file actions
37 lines (32 loc) · 1.04 KB
/
Copy pathSearchInsertPosition.java
File metadata and controls
37 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class SearchInsertPosition {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(searchInsert(new int[] {1, 2, 3, 4, 5}, 3));
System.out.println(searchInsert(new int[] {1, 3, 5, 6}, 2));
System.out.println(searchInsert(new int[] {1, 3, 4, 5}, 6));
System.out.println(searchInsert(new int[] {2}, 3));
System.out.println(searchInsert(new int[] {2}, 1));
}
public static int searchInsert(int[] nums, int target) {
// while the lower bound is less than the upper bound look for the target starting from the
// middle
int mid = (nums.length) / 2;
while (mid > 0 && mid < nums.length) {
if (nums[mid] >= target && nums[mid-1] < target) {
return mid;
} else if (nums[mid] > target) {
mid -= 1;
} else if(nums[mid] < target){
mid += 1;
}
}
if(nums.length==1) {
if(nums[0]>=target) {
return 0;
}else {
return 1;
}
}
return mid;
}
}