-
Notifications
You must be signed in to change notification settings - Fork 0
/
q15_3SUM.py
58 lines (54 loc) · 1.77 KB
/
q15_3SUM.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
class Solution:
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
ans, i = [], 0
nums.sort()
while i < (len(nums) -2):
if nums[i] > 0:
break
j, k = i + 1, len(nums) - 1
while j < k:
s = nums[i] + nums[j] + nums[k]
if s < 0:
j += 1
elif s > 0:
k -= 1
else:
ans.append([nums[i], nums[j], nums[k]])
k -= 1
j += 1
while j < k and nums[j] == nums[j - 1]:
j += 1
while j < k and nums[k] == nums[k + 1]:
k -= 1
i += 1
while i > 0 and i < len(nums)-2 and nums[i] == nums[i-1]:
i += 1
return ans
def threeSum_1(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
using Counter Class
"""
from collections import Counter
counter = Counter(nums)
ans = [] if counter[0] < 3 else [[0, 0, 0]]
ngs = sorted([x for x in counter if x < 0], reverse=True)
n_ngs = sorted([x for x in counter if x >= 0])
for n_ng in n_ngs:
for ng in ngs:
need = -(ng + n_ng)
if need in counter:
if (need == ng or need == n_ng) and counter[need] > 1:
ans.append([ng, need, n_ng])
elif need < ng:
ans.append([need, ng, n_ng])
elif n_ng < need:
ans.append([ng, n_ng, need])
return ans
s=Solution()
print(s.threeSum([0,0,0]))