-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSearchInsertPosition.java
More file actions
36 lines (31 loc) · 1.35 KB
/
Copy pathSearchInsertPosition.java
File metadata and controls
36 lines (31 loc) · 1.35 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
// Given a sorted array and a target value, return the index if the target is found.
// If not, return the index where it would be if it were inserted in order.
// You may assume no duplicates in the array.
// See: https://leetcode.com/problems/search-insert-position/
// See: https://leetcode.com/problems/search-insert-position/discuss/640617/Java-Simple-Binary-Search
package leetcode.binary_search;
public class SearchInsertPosition {
/**
* Simple Binary Search solution
*/
public int searchInsert(int[] nums, int target) {
return binarySearch(nums, target, 0, nums.length - 1);
}
private int binarySearch(int[] nums, int target, int l, int r) {
if (l > r)
return l;
int mid = l + (r - l) / 2;
if (target < nums[mid])
return binarySearch(nums, target, l, mid - 1);
else if(target > nums[mid])
return binarySearch(nums, target, mid + 1, r);
return mid;
}
public static void main(String[] args) {
SearchInsertPosition sln = new SearchInsertPosition();
System.out.println(sln.searchInsert(new int[] {1,3,5,6}, 5));
System.out.println(sln.searchInsert(new int[] {1,3,5,6}, 2));
System.out.println(sln.searchInsert(new int[] {1,3,5,6}, 7));
System.out.println(sln.searchInsert(new int[] {1,3,5,6}, 0));
}
}