添加(0035.搜索插入位置.md):增加typescript版本

This commit is contained in:
Steve2020
2022-06-04 10:44:25 +08:00
parent 9c325284fd
commit cccb4973ae

View File

@ -283,6 +283,28 @@ var searchInsert = function (nums, target) {
};
```
### TypeScript
```typescript
// 第一种二分法
function searchInsert(nums: number[], target: number): number {
const length: number = nums.length;
let left: number = 0,
right: number = length - 1;
while (left <= right) {
const mid: number = Math.floor((left + right) / 2);
if (nums[mid] < target) {
left = mid + 1;
} else if (nums[mid] === target) {
return mid;
} else {
right = mid - 1;
}
}
return right + 1;
};
```
### Swift
```swift