-
Notifications
You must be signed in to change notification settings - Fork 43
/
3sum.py
377 lines (350 loc) · 11.7 KB
/
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
"""
15. 3Sum
Medium
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Example 2:
Input: nums = []
Output: []
Example 3:
Input: nums = [0]
Output: []
Constraints:
0 <= nums.length <= 3000
-105 <= nums[i] <= 105
"""
# V0
# IDEA : for loop + 2 sum
class Solution(object):
def threeSum(self, nums):
# edge case
if not nums or len(nums) < 3:
return []
if len(nums) == 3:
return [nums] if sum(nums) == 0 else []
if nums.count(0) == len(nums):
return [[0,0,0]]
res = []
nums.sort()
# for loop
for i in range(len(nums)):
cur = nums[i]
# 2 sum
d = {}
"""
NOTE !!!
-> we don't need below implementation (will cause TLE)
#_nums = nums[:i] + nums[i+1:]
#for j, x in enumerate(_nums):
-> this one is enough : for j, x in enumerate(nums[i+1:])
"""
for j, x in enumerate(nums[i+1:]):
# cur + x + y = 0
# -> y = -x - cur
if -x-cur in d:
tmp = [cur, x, -x-cur]
tmp.sort()
if tmp not in res:
res.append(tmp)
#res.append([cur, x, -x-cur])
else:
d[x] = j
return res
# V0'
# IDEA : 2 SUM -> 3 SUM
# Time: O(n^2)
# Space: O(1)
# Given an array S of n integers,
# are there elements a, b, c in S such that a + b + c = 0?
# Find all unique triplets in the array which gives the sum of zero.
#
# Note:
# Elements in a triplet (a,b,c) must be in non-descending order. (ie, a <= b <= c)
# The solution set must not contain duplicate triplets.
# For example, given array S = {-1 0 1 2 -1 -4},
#
# A solution set is:
# (-1, 0, 1)
# (-1, -1, 2)
class Solution(object):
def threeSum(self, nums):
if not nums or len(nums) <= 2:
return []
res = []
# optimize, not necessary
nums.sort()
# loop over i
for i in range(len(nums)):
"""
2 sum
"""
d = {}
"""
NOTE !!! here we loop over range(i+1, len(nums))
# -> since we need non duplicated results
"""
for j in range(i+1, len(nums)):
"""
NOTE : nums[i] + nums[j] + nums[k] = 0
# -> so - (nums[i] + nums[j]) = nums[k]
# -> and we are trying to find if such k already in the dict
"""
if -(nums[i] + nums[j]) in d:
tmp = [nums[i]] + [nums[j], -(nums[i]+nums[j])]
tmp.sort()
if tmp not in res:
res.append(tmp)
else:
d[nums[j]] = j
return res
# V0''
# IDEA : 2 SUM -> 3 SUM
class Solution(object):
def threeSum(self, nums):
res = []
if not nums or len(nums) <= 2:
return res
# this sort may not be necessary
nums.sort()
for i in range(len(nums)):
# NOTE : set target = -nums[i]
t = -nums[i]
d = {}
### NOTE : we NEED tp loop from idx = i+1 to len(nums)
for j in range(i+1, len(nums)):
if (-nums[j] + t) in d:
tmp = [ nums[i], nums[j], -nums[j] + t ]
tmp.sort()
# note : this trick to not append duplicated ans
if tmp not in res:
res.append(tmp)
d[nums[j]] = j
return res
# V0''''
# BELOW WILL CAUSE "TIME OUT ERROR"
# due to
# -> _nums = nums[:i] + nums[i+1:]
# -> for j in range(len(_nums))
# class Solution(object):
# def threeSum(self, nums):
# if not nums or len(nums) <= 2:
# return []
# res = []
# nums.sort()
# # loop over i
# for i in range(len(nums)):
# #print("i = " + str(i))
# # updated nums
# _nums = nums[:i] + nums[i+1:]
# # 2 sum
# d = {}
# for j in range(len(_nums)):
# #print("j = " + str(j))
# if -(nums[i]+_nums[j]) in d:
# #return [i, d[target-_nums[i]]]
# #tmp = [i] + [j, d[-nums[i]]]
# tmp = [nums[i]] + [_nums[j], -(nums[i]+_nums[j])]
# tmp.sort()
# if tmp not in res:
# res.append(tmp)
#
# else:
# d[_nums[j]] = j
# return res
# V0''''''
class Solution:
"""
@param numbersbers : Give an array numbersbers of n integer
@return : Find all unique triplets in the array which gives the sum of zero.
"""
def threeSum(self, numbers):
triplets = []
length = len(numbers)
if length < 3:
return triplets
numbers.sort()
for i in range(length):
target = 0 - numbers[i]
# 2 Sum
hashmap = {}
for j in range(i + 1, length):
item_j = numbers[j]
if (target - item_j) in hashmap:
triplet = [numbers[i], target - item_j, item_j]
if triplet not in triplets:
triplets.append(triplet)
else:
hashmap[item_j] = j
return triplets
# V1
# https://algorithm.yuanbin.me/zh-tw/integer_array/3_sum.html?q=
# IDEA : SORT + HASH TABLE + 2 SUM
# IDEA : 2 SUM = 1 SUM + 1 SUM -> 3 SUM = 2 SUM + 1 SUM
class Solution:
"""
@param numbersbers : Give an array numbersbers of n integer
@return : Find all unique triplets in the array which gives the sum of zero.
"""
def threeSum(self, numbers):
triplets = []
length = len(numbers)
if length < 3:
return triplets
numbers.sort()
for i in range(length):
target = 0 - numbers[i]
# 2 Sum
hashmap = {}
for j in range(i + 1, length):
item_j = numbers[j]
if (target - item_j) in hashmap:
triplet = [numbers[i], target - item_j, item_j]
if triplet not in triplets:
triplets.append(triplet)
else:
hashmap[item_j] = j
return triplets
# V1'
# https://blog.csdn.net/fuxuemingzhu/article/details/83115850
# IDEA : SORT + DOUBLE POINTER
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
N = len(nums)
nums.sort()
res = []
for t in range(N - 2):
if t > 0 and nums[t] == nums[t - 1]:
continue
i, j = t + 1, N - 1
while i < j:
_sum = nums[t] + nums[i] + nums[j]
if _sum == 0:
res.append([nums[t], nums[i], nums[j]])
i += 1
j -= 1
while i < j and nums[i] == nums[i - 1]:
i += 1
while i < j and nums[j] == nums[j + 1]:
j -= 1
elif _sum < 0:
i += 1
else:
j -= 1
return res
# V1''
# https://blog.csdn.net/fuxuemingzhu/article/details/83115850
# IDEA : collections.Counter + DOUBLE POINTER
import collections
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
count = collections.Counter(nums)
values = count.keys()
values.sort()
print(values)
N = len(values)
l, r = 0, N - 1
res = list()
visited = set()
for l in range(N):
for r in range(l, N):
t = 0 - values[l] - values[r]
if t in count:
if (t == 0 and count[t] >= 3) \
or (((t == values[l] and t != values[r]) or (t == values[r] and t != values[l])) and count[t] >= 2) \
or (l == r and values[l] != t and count[values[l]] >= 2) \
or (t != values[l] and t != values[r] and l != r):
curlist = sorted([values[l], t, values[r]])
finger = "#".join(map(str, curlist))
if finger not in visited:
res.append(curlist)
visited.add(finger)
return res
# V1'''
# https://www.jiuzhang.com/solution/3sum/#tag-highlight-lang-python
class Solution:
"""
@param numbers: Give an array numbers of n integer
@return: Find all unique triplets in the array which gives the sum of zero.
"""
def threeSum(self, nums):
nums.sort()
results = []
length = len(nums)
for i in range(0, length - 2):
if i and nums[i] == nums[i - 1]:
continue
self.find_two_sum(nums, i + 1, length - 1, -nums[i], results)
return results
def find_two_sum(self, nums, left, right, target, results):
while left < right:
if nums[left] + nums[right] == target:
results.append([-target, nums[left], nums[right]])
right -= 1
left += 1
while left < right and nums[left] == nums[left - 1]:
left += 1
while left < right and nums[right] == nums[right + 1]:
right -= 1
elif nums[left] + nums[right] > target:
right -= 1
else:
left += 1
# V2
# Time: O(n^2)
# Space: O(1)
import collections
class Solution(object):
def threeSum(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
nums, result, i = sorted(nums), [], 0
while i < len(nums) - 2:
if i == 0 or nums[i] != nums[i - 1]:
j, k = i + 1, len(nums) - 1
while j < k:
if nums[i] + nums[j] + nums[k] < 0:
j += 1
elif nums[i] + nums[j] + nums[k] > 0:
k -= 1
else:
result.append([nums[i], nums[j], nums[k]])
j, k = j + 1, k - 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
return result
def threeSum2(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
d = collections.Counter(nums)
nums_2 = [x[0] for x in d.items() if x[1] > 1]
nums_new = sorted([x[0] for x in d.items()])
rtn = [[0, 0, 0]] if d[0] >= 3 else []
for i, j in enumerate(nums_new):
if j <= 0:
numss2 = nums_new[i + 1:]
for x, y in enumerate(numss2):
if 0 - j - y in [j, y] and 0 - j - y in nums_2:
if sorted([j, y, 0 - j - y]) not in rtn:
rtn.append(sorted([j, y, 0 - j - y]))
if 0 - j - y not in [j, y] and 0 - j - y in nums_new:
if sorted([j, y, 0 - j - y]) not in rtn:
rtn.append(sorted([j, y, 0 - j - y]))
return rtn