forked from sashaaero/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |