2 parents 6bdf4fc + 850953c commit 60a42ccCopy full SHA for 60a42cc
1 file changed
other/two-sum.py
@@ -0,0 +1,28 @@
1
+"""
2
+Given an array of integers, return indices of the two numbers such that they add up to a specific target.
3
+
4
+You may assume that each input would have exactly one solution, and you may not use the same element twice.
5
6
+Example:
7
+Given nums = [2, 7, 11, 15], target = 9,
8
9
+Because nums[0] + nums[1] = 2 + 7 = 9,
10
+return [0, 1].
11
12
13
+def twoSum(nums, target):
14
+ """
15
+ :type nums: List[int]
16
+ :type target: int
17
+ :rtype: List[int]
18
19
+ chk_map = {}
20
+ for index, val in enumerate(nums):
21
+ compl = target - val
22
+ if compl in chk_map:
23
+ indices = [chk_map[compl], index]
24
+ print(indices)
25
+ return [indices]
26
+ else:
27
+ chk_map[val] = index
28
+ return False
0 commit comments