我一直在尝试向我的 python 字典(表)添加重复键以解决“二和”问题。
给定一个整数数组,返回两个数字的索引,使它们相加为特定目标。
我现在意识到这是不可能做到的,并且会很感激关于如何在没有蛮力的情况下解决这个问题的任何想法或建议。请记住,我这周开始尝试学习 Python。所以我很抱歉有一个简单的解决方案
numbers = [0, 0, 0, 0, 0, 0, 0] # initial list
target = 6 # The sum of two numbers within list
# Make list into dictionary where the given values are used as keys and
actual values are indices
table = {valueKey: index for index, valueKey in enumerate(numbers)}
print(table)
>>> {0: 6}
白衣染霜花
HUH函数
相关分类