class Solution:
# @return a list of lists of length 4, [[val1,val2,val3,val4]]
def fourSum(self, num, target):
numLen, res, dict = len(num), set(), {}
if numLen < 4: return []
num.sort()
for p in range(numLen):
for q in range(p+1, numLen):
if num[p]+num[q] not in dict:
dict[num[p]+num[q]] = [(p,q)]
else:
dict[num[p]+num[q]].append((p,q))
for i in range(numLen):
for j in range(i+1, numLen-2):
T = target-num[i]-num[j]
if T in dict:
for k in dict[T]:
if k[0] > j: res.add((num[i],num[j],num[k[0]],num[k[1]]))
return [list(i) for i in res]
4 Sum
最新推荐文章于 2024-01-25 00:16:43 发布