-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeSum.py
More file actions
80 lines (72 loc) · 2.54 KB
/
Copy paththreeSum.py
File metadata and controls
80 lines (72 loc) · 2.54 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
"""
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
"""
class Solution:
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) < 3:
return []
res = []
i = 0
nums.sort()
last_num = nums[0] - 1
while i < len(nums) and nums[i] < 1:
if nums[i] == last_num:
i += 1
continue
begin = i + 1
end = len(nums) - 1
while begin < end:
if nums[begin] + nums[end] + nums[i] > 0:
temp = nums[end]
while end >= begin and nums[end] == temp:
end -= 1
elif nums[begin] + nums[end] + nums[i] < 0:
temp = nums[begin]
while begin < end and nums[begin] == temp:
begin += 1
else:
res.append([nums[i], nums[begin], nums[end]]) # 这个顺序无关
temp = nums[begin]
while begin < end and nums[begin] == temp:
begin += 1
last_num = nums[i]
i += 1
return res
def threeSum2(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
dic = {}
for ele in nums:
dic[ele] = dic.get(ele, 0) + 1
neg = sorted(filter(lambda x: x < 0, dic))
pos = sorted(filter(lambda x: x >= 0, dic))
if 0 in dic and dic[0] > 2:
res = [[0, 0, 0]]
else:
res = []
for ele1 in neg:
for ele2 in pos:
tar = -(ele1 + ele2)
if tar in dic:
if tar in (ele1, ele2) and dic[tar] > 1:
res.append([ele1, tar, ele2])
elif tar < ele1 or tar > ele2: # 这里写法不是唯一的,要保证两个子句所指示的区间相互不覆盖。在此条件下,找寻tar的方向总是一样的,可以保证唯一性。
res.append([ele1, tar, ele2])
return res
if __name__ == '__main__':
s = Solution()
print(s.threeSum([-1, 0, 1, 2, -1, -4]))