#第一种方法#
#时间换取空间:两个循环求解#
def search(nums,target):for i in nums:start = nums.index(i) + 1for j in nums[start: ]:if i + j == target:result = [nums.index(i)]next_index = nums[start:].index(j) + startresult.append(next_index)return result
if __name__ == '__main__':print(search([3,2,4],6))
#第二种方法#
#空间换取时间:引入字典快速求解#
def search(nums,target):resut_list = list()result_dict = {}result_dict[nums[0]] = 0for i in nums[1:]:key = target - iif key in result_dict.keys():resut_list.append(result_dict[key])resut_list.append(nums.index(i))else:result_dict[i] = nums.index(i)return resut_list
if __name__ == '__main__':print(search([3,2,4],6))
有兴趣的小伙伴也可以思考一下,如何返回数组里满足条件的所有值的组合。