Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Latest
- class Solution:
- def twoSum(self, nums: List[int], target: int) -> List[int]:
- from collections import defaultdict
- hashmap = defaultdict(int)
- for index, num in enumerate(nums):
- if (target - num) in hashmap:
- return [hashmap[target-num], index]
- hashmap[num] = index
- # Earlier
- class Solution:
- def twoSum(self, nums: List[int], target: int) -> List[int]:
- hashset = {}
- for i, num in enumerate(nums):
- if (target - num) in hashset:
- return [i, hashset[target - num]]
- hashset[num] = i
- # Note that the below solution does not work since we expect both indices to be different
- class Solution:
- def twoSum(self, nums: List[int], target: int) -> List[int]:
- from collections import defaultdict
- hashmap = defaultdict(int)
- for index, num in enumerate(nums):
- if (target - num) in hashmap:
- return [hashmap[target-num], index]
- hashmap[num] = index
Advertisement
Add Comment
Please, Sign In to add comment