Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. class Solution(object):
  2. def twoSum(self, nums, target):
  3. """
  4. :type nums: List[int]
  5. :type target: int
  6. :rtype: List[int]
  7. """
  8. # diff contains (key, value) pairs
  9. # key - the difference between target number and the current number
  10. # value - the index of the number
  11. diff = {}
  12. for index in range(len(nums)):
  13. if nums[index] in diff:
  14. return [diff[nums[index]], index]
  15.  
  16. # to avoid duplicate keys problem
  17. # though the problem has restricted it
  18.  
  19. #elif target - nums[index] in diff:
  20. #continue
  21.  
  22. else:
  23. diff[target - nums[index]] = index
  24.  
  25. return None
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement