File tree Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Expand file tree Collapse file tree 2 files changed +25
-0
lines changed Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments