LeetCode 0001 Two Sum
2022.07.18

問題

python

python
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        dict = defaultdict(int)
        for i in range(len(nums)):
            if target - nums[i] in dict:
                return [i, dict[target-nums[i]]]
            dict[nums[i]] = i
  • defaultdict