Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 5bf6df3

Browse files
Merge pull request fnplus#576 from Shreyansh0001/master
Create TwoSum.py
2 parents 2fbac81 + a3683e3 commit 5bf6df3

File tree

1 file changed

+27
-0
lines changed
  • Company Specific Interview Questions/Airbnb/Solutions/Python

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
def twoSum(nums, target):
2+
"""
3+
:type nums: List[int]
4+
:type target: int
5+
:rtype: List[int]
6+
"""
7+
index_map = {}
8+
for i in range(len(nums)):
9+
num = nums[i]
10+
pair = target - num
11+
if pair in index_map:
12+
return [index_map[pair], i]
13+
index_map[num] = i
14+
return None
15+
16+
nums = [1,4,8,3,2,9,15]
17+
target = 13
18+
print("Nums: ", nums)
19+
print("Target: ", target)
20+
print("Solution: ", twoSum(nums, target))
21+
22+
print()
23+
nums = [2,7,11,15]
24+
target = 9
25+
print("Nums: ", nums)
26+
print("Target: ", target)
27+
print("Solution: ", twoSum(nums, target))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /