Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions arai60/41-44_Binary_Search/41_35_Search Insert Position/level_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# モジュールを利用
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
return bisect_left(nums, target)


# 2分探索を自分で実装
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums)
while left < right:
middle = (left + right) // 2
if nums[middle] < target:
left = middle + 1
else:
right = middle
return left
14 changes: 14 additions & 0 deletions arai60/41-44_Binary_Search/41_35_Search Insert Position/level_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# level_1から特に変更なし
# bisectモジュールのソースとも同じ実装
# https://github.com/python/cpython/blob/3.12/Lib/bisect.py#L74
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums)
while left < right:
middle = (left + right) // 2
if nums[middle] < target:
left = middle + 1
else:
right = middle
return left
11 changes: 11 additions & 0 deletions arai60/41-44_Binary_Search/41_35_Search Insert Position/level_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums)
while left < right:
middle = (left + right) // 2
if nums[middle] < target:
left = middle + 1
else:
right = middle
return left