mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
添加(0035.搜索插入位置.md):增加typescript版本
This commit is contained in:
@ -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
|
||||
|
Reference in New Issue
Block a user