LeetCode Two Sum两数之和 (python3)

class Solution(object):
    def twoSum(self, nums, target):
        buff_dict = {}
        for i in range(len(nums)):
            if nums[i] in buff_dict:
                return [buff_dict[nums[i]], i]
            else:
                buff_dict[target - nums[i]] = i

Runtime: 36 ms, faster than 99.40% of Python3 online submissions for
Two Sum. Memory Usage: 14.8 MB, less than 5.08% of Python3 online
submissions for Two Sum.

e.g. nums = [1,5,9,2,10] target = 3

i buff_dict
0 3-1:0
1 3-5:1
2 3-9:2
3 发现nums[3]=2在dict中

你可能感兴趣的:(LeetCode Two Sum两数之和 (python3))