File tree Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Expand file tree Collapse file tree 2 files changed +20
-1
lines changed Original file line number Diff line number Diff line change 22
22
| 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 ) |
23
23
| 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 ) |
24
24
| 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 )] ( ) |
26
26
| 2 | [ ] ( ) | Easy | |
27
27
| 2 | [ ] ( ) | Easy | |
28
28
| 2 | [ ] ( ) | Easy | |
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments