Skip to content

Commit

Permalink
leet code two-sum solution
Browse files Browse the repository at this point in the history
id 1

Relates sashaaero#1
  • Loading branch information
Filipe Almeida committed Oct 10, 2019
1 parent bccf0d6 commit 34b5eba
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions leet_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# https://leetcode.com/problems/two-sum
# Runtime: 32 ms, faster than 90.53% of Python online submissions for Two Sum.

class Solution(object):
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
h = {}
for i, num in enumerate(nums):
n = target - num
if n not in h:
h[num] = i
else:
return [h[n], i]

if __name__ == '__main__':
s = Solution()
assert s.twoSum([2,7,11,15], 9) == [0,1]

0 comments on commit 34b5eba

Please sign in to comment.