forked from lymin/python_interview_question
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path29.py
More file actions
36 lines (34 loc) · 936 Bytes
/
29.py
File metadata and controls
36 lines (34 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# 1
class Solution:
def two_sum(self, nums, target):
'''
:nums: List[int]
:target: int
:return: List[int]
'''
d = {}
size = 0
while size < len(nums):
if target - nums[size] in d:
if d[target - nums[size]] < size:
return [d[target - nums[size]], size]
else :
d[nums[size]] = size
size = size + 1
# 2
from typing import List
# 用于类型检查,防止运行时出现参数与返回值不符合
def two_sum(nums: List[int], target:int) -> List[int]:
s = {}
for i, n in enumerate(nums):
if (target - n) in s :
return [s[target - n], i]
else:
s[n] = i
if __name__ == "__main__":
#solu = Solution()
num = [2, 7, 2, 11, 15]
target = 9
nums = two_sum(num, target)
#nums = solu.two_sum(num, target)
print(nums)