File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 30
30
28|[ Implement strStr()] ( ./0028-implement-strstr.js ) |Easy|
31
31
31|[ Next Permutation] ( ./0031-next-permutation.js ) |Medium|
32
32
33|[ Search in Rotated Sorted Array] ( ./0033-search-in-rotated-sorted-array.js ) |Medium|
33
+ 35|[ Search Insert Position] ( ./0035-search-insert-position.js ) |Easy|
33
34
36|[ Valid Sudoku] ( ./0036-valid-sudoku.js ) |Medium|
34
35
41|[ First Missing Positive] ( ./0041-first-missing-positive.js ) |Hard|
35
36
42|[ Trapping Rain Water] ( ./0042-trapping-rain-water.js ) |Hard|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 35. Search Insert Position
3
+ * https://leetcode.com/problems/search-insert-position/
4
+ * Difficulty: Easy
5
+ *
6
+ * Given a sorted array of distinct integers and a target value, return the
7
+ * index if the target is found. If not, return the index where it would be
8
+ * if it were inserted in order.
9
+ *
10
+ * You must write an algorithm with O(log n) runtime complexity.
11
+ */
12
+
13
+ /**
14
+ * @param {number[] } nums
15
+ * @param {number } target
16
+ * @return {number }
17
+ */
18
+ var searchInsert = function ( nums , target ) {
19
+ let start = 0 ;
20
+ let end = nums . length - 1 ;
21
+
22
+ while ( start <= end ) {
23
+ const middle = Math . floor ( ( start + end ) / 2 ) ;
24
+ if ( target > nums [ middle ] ) {
25
+ start = middle + 1 ;
26
+ } else {
27
+ end = middle - 1 ;
28
+ }
29
+ }
30
+
31
+ return start ;
32
+ } ;
You can’t perform that action at this time.
0 commit comments