Skip to content

Commit 1173aef

Browse files
solves search insert position
1 parent 5d1c0f1 commit 1173aef

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
| 26 | [Remove Duplicates From Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/RemoveDuplicatesFromSortedArray.java) |
2323
| 27 | [Remove Element](https://leetcode.com/problems/remove-element/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/RemoveElement.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/python/remove_element.py)|
2424
| 28 | [Needle in Haystack](https://leetcode.com/problems/implement-strstr) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/NeedleInHaystack.java) [![Python](https://img.icons8.com/color/35/000000/python.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/python/needle_in_haystack.py)|
25-
| 2 | []() | Easy | |
25+
| 35 | [Search Inserted Position](https://leetcode.com/problems/search-insert-position/) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)]() |
2626
| 2 | []() | Easy | |
2727
| 2 | []() | Easy | |
2828
| 2 | []() | Easy | |

src/SearchInsertPosition.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
public class SearchInsertPosition {
2+
public int searchInsert(int[] nums, int target) {
3+
return binarySearchNextLargest(nums, target);
4+
}
5+
6+
private int binarySearchNextLargest(int[] array, int val) {
7+
if (array == null || array.length == 0) return 0;
8+
9+
int left = 0, right = array.length - 1, middle;
10+
while (left <= right) {
11+
middle = left + (right - left) / 2;
12+
if (array[middle] == val) return middle;
13+
else if (array[middle] > val) right = middle - 1;
14+
else left = middle + 1;
15+
}
16+
17+
return left;
18+
}
19+
}

0 commit comments

Comments
 (0)