Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 886ad35

Browse files
committedDec 30, 2021
Add solution #35
1 parent 6a6f921 commit 886ad35

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
28|[Implement strStr()](./0028-implement-strstr.js)|Easy|
3131
31|[Next Permutation](./0031-next-permutation.js)|Medium|
3232
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|
3334
36|[Valid Sudoku](./0036-valid-sudoku.js)|Medium|
3435
41|[First Missing Positive](./0041-first-missing-positive.js)|Hard|
3536
42|[Trapping Rain Water](./0042-trapping-rain-water.js)|Hard|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.