-
Notifications
You must be signed in to change notification settings - Fork 481
/
0015.py
30 lines (26 loc) · 915 Bytes
/
0015.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
class Solution:
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums_hash = {}
result = list()
for num in nums:
nums_hash[num] = nums_hash.get(num, 0) + 1
if 0 in nums_hash and nums_hash[0] >= 3:
result.append([0, 0, 0])
neg = list(filter(lambda x: x < 0, nums_hash))
pos = list(filter(lambda x: x>= 0, nums_hash))
for i in neg:
for j in pos:
dif = 0 - i - j
if dif in nums_hash:
if dif in (i, j) and nums_hash[dif] >= 2:
result.append([i, j, dif])
if dif < i or dif > j:
result.append([i, j, dif])
return result
if __name__ == '__main__':
nums = [3,0,-2,-1,1,2]
print(Solution().threeSum(nums))