Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution(object):
- def twoSum(self, nums, target):
- """
- :type nums: List[int]
- :type target: int
- :rtype: List[int]
- """
- # diff contains (key, value) pairs
- # key - the difference between target number and the current number
- # value - the index of the number
- diff = {}
- for index in range(len(nums)):
- if nums[index] in diff:
- return [diff[nums[index]], index]
- # to avoid duplicate keys problem
- # though the problem has restricted it
- #elif target - nums[index] in diff:
- #continue
- else:
- diff[target - nums[index]] = index
- return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement