1:
"""
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
Given nums = [2, 7, 11, 15], target = 9,
Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].
"""
def solve_ways(arrays,res):
dic=dict()
for k,v in enumerate(arrays):
s = res-v
if s in dic:
print [k,dic[s]]
else:
dic[v]=k
solve_ways([1,2,3,6],7)
两数之和问题求解
本文介绍了一种解决两数之和问题的有效算法。给定一个整数数组及目标值,通过一次遍历找到两个数,使得这两个数相加等于目标值。此算法采用字典来存储已遍历过的元素及其索引,从而实现快速查找。
584

被折叠的 条评论
为什么被折叠?



