- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
所以我写了一个二进制搜索算法,但是当我测试运行时它并不能完美地工作。
这是代码
def binarySearch(lst, target):
low = 0
high = len(lst)-1
while high >= low:
mid = (high + low)//2
if target < lst[mid]:
high = mid - 1
elif target > lst[mid]:
low = mid + 1
else:
return mid
return (-1 * (mid+1))
abd这里是调用函数的代码
lst_test = [3, 4, 6, 7]
target_values = [1, 3, 5, 8]
for t in target_values:
i = binarySearch(lst_test, t)
if (i < 0):
print("In", lst_test, t, "is going to be inserted at index",-1*(i+1))
lst_test.insert((i+1)*-1, t)
else:
print("In", lst_test, t, "was found at index", i)
print("The final list is:", lst_test)
问题是这样的,当我实际运行它给出的函数时,我想将列表 target_values 添加到第一个正确的顺序中
In [3, 4, 6, 7] 1 is going to be inserted at index 0
In [1, 3, 4, 6, 7] 3 was found at index 1
In [1, 3, 4, 6, 7] 5 is going to be inserted at index 3
In [1, 3, 4, 5, 6, 7] 8 is going to be inserted at index 5
The final list is: [1, 3, 4, 5, 6, 8, 7]
这很奇怪,它可以工作,但只在调用的最后部分失败有什么办法可以解决这个问题吗?最终列表应该是 [1,3,4,5,6,7,8]
根据要求,我跟踪了我的二进制搜索算法,它的质量很差。我希望这会有所帮助
Mid point is: 1
target value is smaller than a mid point
Mid point is: 0
target value is smaller than a mid point
In [3, 4, 6, 7] 1 is going to be inserted at index 0
Mid point is: 2
target value is smaller than a mid point
Mid point is: 0
target value is larger than a mid point
Mid point is: 1
Found the index location at 1
In [1, 3, 4, 6, 7] 3 was found at index 1
Mid point is: 2
target value is larger than a mid point
Mid point is: 3
target value is smaller than a mid point
In [1, 3, 4, 6, 7] 5 is going to be inserted at index 3
Mid point is: 2
target value is larger than a mid point
Mid point is: 4
target value is larger than a mid point
Mid point is: 5
target value is larger than a mid point
In [1, 3, 4, 5, 6, 7] 8 is going to be inserted at index 5
The final list is: [1, 3, 4, 5, 6, 8, 7]
最佳答案
只需将函数更改为返回 (-1 * (low+1))
即可:
def binarySearch(lst, target):
low = 0
high = len(lst)-1
while high >= low:
mid = (high + low)//2
if target < lst[mid]:
high = mid - 1
elif target > lst[mid]:
low = mid + 1
else:
return mid
return (-1 * (low+1))
输出:
('In', [3, 4, 6, 7], 1, 'is going to be inserted at index', 0)
('In', [1, 3, 4, 6, 7], 3, 'was found at index', 1)
('In', [1, 3, 4, 6, 7], 5, 'is going to be inserted at index', 3)
('In', [1, 3, 4, 5, 6, 7], 8, 'is going to be inserted at index', 6)
('The final list is:', [1, 3, 4, 5, 6, 7, 8])
原始实现的问题是代码假设 mid
是插入索引,但它永远不会超出循环中的当前列表,因为当值被插入到列表末尾时它应该.
关于python - 二进制搜索算法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40901682/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!