Fix binary search.

Finetune a figure in intro_to_dp.
This commit is contained in:
krahets
2023-07-01 20:07:21 +08:00
parent d8b2eb1a32
commit 69920a0599
6 changed files with 6 additions and 6 deletions

View File

@ -32,7 +32,7 @@ def binary_search_right_edge(nums: list[int], target: int) -> int:
j = m - 1 # target 在区间 [i, m-1] 中
else:
i = m + 1 # 首个大于 target 的元素在区间 [m+1, j] 中
if j == len(nums) or nums[j] != target:
if j < 0 or nums[j] != target:
return -1 # 未找到目标元素,返回 -1
return j