-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_两数之和.py
65 lines (53 loc) · 1.76 KB
/
1_两数之和.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
'''
class Solution(object):
def twoSum(self, nums, target):
"""
时间复杂度 O(n^2)
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
length = len(nums)
if length < 2:
return None
for i in range(length - 1):
for j in range(i + 1, length):
if nums[i] + nums[j] == target:
return [i, j]
return None
def twoSum_2(self, nums, target):
length = len(nums)
if length < 2:
return None
d = {}
for index, element in enumerate(nums):
another_num = target - nums[index]
for key, value in d.items():
if another_num == value:
return [key, index]
d[index] = element
return None
def twoSum_3(self, nums, target):
'''
时间复杂度最低 O(n),关键是在写入字典的时候,键和值是反过来的
'''
length = len(nums)
if length < 2:
return None
d = {}
for index, element in enumerate(nums):
another_num = target - nums[index]
# 时间复杂度O(1)
if another_num in d.keys():
return [d[another_num], index]
else:
d[element] = index
return None
if __name__ == '__main__':
s = Solution()
nums = [2, 7, 11, 15]
target = 9
print(s.twoSum_3(nums, target))