We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 9723639 commit dd9b545Copy full SHA for dd9b545
000. Two Sum/README.md
@@ -11,4 +11,8 @@
11
12
既然是配对,那么 kv 结构是非常合适的,于是上了 Hash 表。让 key 就是要找的 `b`,让 value 记录 `a` 的 index,也就是结果需要的索引。
13
14
-这样思路就很简单明了了。
+这样思路就很简单明了了。
15
+
16
+## Python
17
18
+基本与 C++ 的思路一致,只不过更简洁了。
000. Two Sum/solution.py
@@ -0,0 +1,24 @@
1
+#!python3
2
3
4
+class Solution:
5
+ def twoSum(self, nums, target):
6
+ """
7
+ :type nums: List[int]
8
+ :type target: int
9
+ :rtype: List[int]
10
+ dic = {}
+ for index, num in enumerate(nums):
+ if num in dic:
+ return [dic[num], index]
+ dic[target - num] = index
+if __name__ == "__main__":
19
+ nums = [2, 7, 11, 15]
20
+ target = 9
21
+ assert (Solution().twoSum(nums, target) == [0, 1])
22
+ nums = [3, 2, 4]
23
+ target = 6
24
+ assert (Solution().twoSum(nums, target) == [1, 2])
0 commit comments