Skip to content

Commit a0638ea

Browse files
authored
Update 35-search-insert-position.js
1 parent 18b4bb3 commit a0638ea

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

35-search-insert-position.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,22 @@ const searchInsert = function(nums, target) {
1414
}
1515
}
1616
};
17+
18+
// another
19+
20+
/**
21+
* @param {number[]} nums
22+
* @param {number} target
23+
* @return {number}
24+
*/
25+
const searchInsert = function(nums, target) {
26+
const n = nums.length
27+
let l = 0, r = n - 1
28+
while(l <= r) {
29+
const mid = l + ((r - l) >> 1)
30+
if(nums[mid] === target) return mid
31+
if(nums[mid] > target) r = mid - 1
32+
else l = mid + 1
33+
}
34+
return l
35+
};

0 commit comments

Comments
 (0)