暴力法解决
def add_result(nums,target):
result = []
for i in range(0,len(nums)):
for j in range(1,len(nums)):
if nums[i] + nums[j] == target:
result.append(i)
result.append(j)
return result
print(add_result(nums,target))
哈希法
def add_hash(nums,target):
result = []
hash = {}
hash = dict(zip(range(0,10),nums))
for i in range(len(nums)):
diff = target - nums[i]
if diff in hash and nums[i] != target:
result.append(hash.get(diff))
result.append(i)
return result
print(add_hash(nums,target))