Skip to content

Commit 0254bc6

Browse files
committed
Update solution 015 [Python3]
1 parent 2fd42d5 commit 0254bc6

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

solution/015.3Sum/Solution.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution:
2+
def threeSum(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[List[int]]
6+
"""
7+
dic = {}
8+
for ele in nums:
9+
dic[ele] = dic.get(ele,0) + 1
10+
neg = sorted(filter(lambda x:x<0,dic))
11+
pos = sorted(filter(lambda x:x>=0,dic))
12+
if(0 in dic and dic[0]>2):
13+
res = [[0,0,0]]
14+
else:
15+
res = []
16+
for ele1 in neg:
17+
for ele2 in pos:
18+
tar = -(ele1 + ele2)
19+
if(tar in dic):
20+
if(tar in (ele1,ele2) and dic[tar]>1):
21+
res.append([ele1,tar,ele2])
22+
elif(tar < ele1 or tar > ele2):
23+
res.append([ele1,tar,ele2])
24+
return res

0 commit comments

Comments
 (0)