Skip to content

Commit d7a74e3

Browse files
committed
add two sum algorithm
1 parent fa712df commit d7a74e3

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

arrays/two_sum.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
two_sum([2, 7, 11, 15], 18) => [7, 11]
3+
two_sum([2, 7, 11, 15], 9) => [2, 7]
4+
complexity: O(n)
5+
"""
6+
7+
from typing import List
8+
9+
10+
def two_sum(numbers: List[int], target: int) -> List[int]:
11+
for first, second in zip(numbers, numbers[1:]):
12+
if first + second == target:
13+
return [first, second]
14+
return []
15+
16+
17+
if __name__ == "__main__":
18+
print(two_sum([2, 7, 11, 15], 18) == [7, 11])
19+
print(two_sum([2, 7, 11, 15], 9) == [2, 7])

0 commit comments

Comments
 (0)