数组-(Sum)找到和为k的两个数字

'''
找出数组中和为某个数的两个数的序号
思路:和为k等价与k-a=b,建立字典,遍历每个元素ai
如果k-ai不存在字典中就记下ai的位置i来,到下一个数如果k-aj=ai在字典中,那么结果就是(i,j)
'''


class Solution:
    def call(self, nums, target):
        hashset = {}
        for i, m in enumerate(nums):
            if target - m not in hashset:
                hashset[m] = i
            else:
                return (hashset[target - m] , i )


s = Solution()
print(s.call([2, 5, 7, 11, 15], 9))

#(0, 2)
原文地址:https://www.cnblogs.com/onenoteone/p/12441702.html