0035.搜索插入位置

This commit is contained in:
aouos
2021-05-25 13:59:15 +08:00
parent e9f8cdacfc
commit 02381a3aa1

View File

@ -257,7 +257,25 @@ class Solution:
Go
JavaScript:
```js
var searchInsert = function (nums, target) {
let l = 0, r = nums.length - 1, ans = nums.length;
while (l <= r) {
const mid = l + Math.floor((r - l) >> 1);
if (target > nums[mid]) {
l = mid + 1;
} else {
ans = mid;
r = mid - 1;
}
}
return ans;
};
```
-----------------------