Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cccb497

Browse files
添加(0035.搜索插入位置.md):增加typescript版本
1 parent 9c32528 commit cccb497

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎problems/0035.搜索插入位置.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,28 @@ var searchInsert = function (nums, target) {
283283
};
284284
```
285285

286+
### TypeScript
287+
288+
```typescript
289+
// 第一种二分法
290+
function searchInsert(nums: number[], target: number): number {
291+
const length: number = nums.length;
292+
let left: number = 0,
293+
right: number = length - 1;
294+
while (left <= right) {
295+
const mid: number = Math.floor((left + right) / 2);
296+
if (nums[mid] < target) {
297+
left = mid + 1;
298+
} else if (nums[mid] === target) {
299+
return mid;
300+
} else {
301+
right = mid - 1;
302+
}
303+
}
304+
return right + 1;
305+
};
306+
```
307+
286308
### Swift
287309

288310
```swift

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /