Skip to content

Commit f32b60e

Browse files
merge
2 parents 9d29d38 + 65b92e8 commit f32b60e

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

0001/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Two sum
2+
3+
### Problem statemen
4+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
5+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
6+
7+
You can return the answer in any order.

0001/two_sum.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
hash_table = dict()
9+
10+
# Iterating the list
11+
for i, num in enumerate(nums):
12+
13+
# If the target-num in dict then, we found the solution
14+
try:
15+
hash_table[target - num]
16+
return [hash_table[target - num], i]
17+
except KeyError:
18+
hash_table[num] = i

0 commit comments

Comments
 (0)