-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathThreeSum.py
More file actions
54 lines (53 loc) · 1.63 KB
/
Copy pathThreeSum.py
File metadata and controls
54 lines (53 loc) · 1.63 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
def threeSum(nums: List[int]) -> List[List[int]]:
nums.sort()
index = 1
x = nums[0]
counter = 1
while index < len(nums):
if nums[index] == x:
counter = counter + 1
if x == 0:
if counter > 3:
nums.pop(index)
else:
index = index + 1
elif counter > 2:
nums.pop(index)
else:
index = index + 1
else:
x = nums[index]
counter = 1
index = index + 1
if(not all(n>0 for n in nums)):
counts = Counter(nums)
result = [[0,0,0]] if counts[0] >= 3 else []
nums =[i for i in sorted(counts) if i!=0]
if counts[0]>0:
for i in nums:
if i>0:
break
if -i in counts:
result.append([-i , 0 , i])
for i in nums:
if i & 1:
continue
remaining = -i >> 1
if counts[remaining] >= 2:
result.append([i, remaining, remaining])
for i, n in enumerate(nums):
kk = -(nums[0] + n)
if kk < n:
break
j = bisect_right(nums, -n << 1) if n < 0 else i + 1
k = bisect_right(nums, kk)
for right in nums[j:k]:
left = -(n + right)
if left in counts:
result.append([left, n, right])
del counts , nums
return result
else:
return []
open('user.out', 'w').write("".join(str(threeSum(loads(line))) + "\n" for line in stdin))
exit()