Question
Solution
讨论区的 solution O( n 2 n^2 n2)
class Solution():
def threeSum(self, nums):
def twoSum(nums, i, total):
#res_set = set()
res_set = []
b = i
e = len(nums)-1
while(b < e):
if nums[b] + nums[e] == total:
res_set.append( [total*-1, nums[b], nums[e]] )
b += 1
while(nums[b] == nums[b-1] and b < e):
b += 1
e -= 1
elif nums[b] + nums[e] < total:
b += 1
else:
e -= 1
return res_set
nums.sort()
res = []
for indx, n in enumerate(nums):
if indx == 0 or (indx != 0 and n != nums[indx-1]):
temp = twoSum(nums, indx+1, n*-1)
if len(temp) >0:
res += temp
return res