# 给你一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?
# 请你找出所有满足条件且不重复的三元组。
# 注意:答案中不可以包含重复的三元组。
# 示例:
# 给定数组 -1 0 1 2 -1 -4
# 满足要求的三元组集合为:
# -1 0 1
# -1 -1 2
def find_three_nums(l):
nums_list = []
for i in range(0,len(l)-2):
for j in range(i+1,len(l)-1):
for p in range(j+1,len(l)):
if l[i]+l[j]+l[p]==0:
three_nums = [l[i],l[j],l[p]]
three_nums.sort()
if three_nums not in nums_list:
nums_list.append(three_nums)
print(nums_list)
l = [-1,0,1,2,-1,-4]
find_three_nums(l)
奇妙的三元组(Python实现)
最新推荐文章于 2024-07-31 15:55:27 发布