问题描述
给定一个由 n 个整数组成的数组 nums,nums 中是否有元素 a、b、c 使得 a + b + c = 0?找出数组中所有唯一的三元组,其总和为零。
注意解集不能包含重复的三元组。
下面是我找到三元组的代码。我找不到删除重复三胞胎模式的解决方案
如果 I/P 是 -1 0 1 0 输出必须只有 [[-1,1]] 但我得到的是 [[1,-1,0],[0,1]] ....有什么想法吗?
class Solution:
def threeSum(self,nums):
result = []
for i in range (2,len(nums)):
# i,j and k will be the pointers to the list
k = i - 1
j = i - 2
for k in range (k,-1):
for j in range (j,-1):
# verify if the summation is 0
if j < k and nums[i]+nums[j]+nums[k]== 0:
result.append([nums[i],nums[j],nums[k]])
unique_list = []
# logic to find unique list elements
# this works only when the list patterns are same
for x in result:
if x not in unique_list:
unique_list.append(x)
return unique_list
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)