8000 added python solution for 000. · binEncoder/LeetCode@dd9b545 · GitHub
[go: up one dir, main page]

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

Appearance settings

Commit dd9b545

Browse files
committed
added python solution for 000.
1 parent 9723639 commit dd9b545

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

000. Two Sum/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@
1111

1212
既然是配对,那么 kv 结构是非常合适的,于是上了 Hash 表。让 key 就是要找的 `b`,让 value 记录 `a` 的 index,也就是结果需要的索引。
1313

14-
这样思路就很简单明了了。
14+
这样思路就很简单明了了。
15+
16+
## Python
17+
18+
基本与 C++ 的思路一致,只不过更简洁了。

000. Two Sum/solution.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
"""
11+
dic = {}
12+
for index, num in enumerate(nums):
13+
if num in dic:
14+
return [dic[num], index]
15+
dic[target - num] = index
16+
17+
18+
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

Comments
 (0)
0