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 54579e9

Browse files
Merge pull request youngyangyang04#2480 from alfie-chen/master
Update 0035.搜索插入位置 - 添加Python3 第二種二分法 - 左闭右开
2 parents 9287b4d + 2021673 commit 54579e9

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@ impl Solution {
332332
### Python
333333

334334
```python
335+
# 第一种二分法: [left, right]左闭右闭区间
335336
class Solution:
336337
def searchInsert(self, nums: List[int], target: int) -> int:
337338
left, right = 0, len(nums) - 1
@@ -348,6 +349,26 @@ class Solution:
348349
return right + 1
349350
```
350351

352+
```python
353+
# 第二种二分法: [left, right)左闭右开区间
354+
class Solution:
355+
def searchInsert(self, nums: List[int], target: int) -> int:
356+
left = 0
357+
right = len(nums)
358+
359+
while (left < right):
360+
middle = (left + right) // 2
361+
362+
if nums[middle] > target:
363+
right = middle
364+
elif nums[middle] < target:
365+
left = middle + 1
366+
else:
367+
return middle
368+
369+
return right
370+
```
371+
351372
### JavaScript
352373

353374
```js

0 commit comments

Comments
(0)

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