Skip to content

Commit 681b259

Browse files
Add files via upload
1 parent db10d68 commit 681b259

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 二分查找法
2+
# 36ms 98.77%
3+
class Solution:
4+
def searchInsert(self, nums, target):
5+
"""
6+
:type nums: List[int]
7+
:type target: int
8+
:rtype: int
9+
"""
10+
left, right = 0, len(nums) - 1
11+
while right - left > 1:
12+
mid = (left + right) // 2
13+
if nums[mid] <= target:
14+
left = mid
15+
else:
16+
right = mid - 1
17+
18+
if target <= nums[left]:
19+
return left
20+
elif target <= nums[right]:
21+
return left + 1
22+
else:
23+
return right + 1

0 commit comments

Comments
 (0)