18. 四数之和
给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):
0 <= a, b, c, d < n
a、b、c 和 d 互不相同
nums[a] + nums[b] + nums[c] + nums[d] == target
你可以按 任意顺序 返回答案 。
示例 1:
输入:nums = [1,0,-1,0,-2,2], target = 0
输出:[[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
示例 2:
输入:nums = [2,2,2,2,2], target = 8
输出:[[2,2,2,2]]
提示:
1 <= nums.length <= 200
-109 <= nums[i] <= 109
-109 <= target <= 109
代码
def numsum(nums,target):
d = {}
for num in nums:
d[num]=d.get(num,0)+1
ans=set()
for i in range(len(nums)):
for j in range(i+1,len(nums)):
for k in range(j+1,len(nums)):
val=target-(nums[i]+nums[j]+nums[k])
if val in d:
count= (nums[i] == val) + (nums[j] == val) + (nums[k] == val)
if d[val] > count:
ans.add(tuple(sorted([nums[i], nums[j], nums[k], val])))
print([list(x) for x in ans])
return
if __name__=='__main__':
nums=input("nums=").split()
nums=[int(x) for x in nums]
target=int(input("target="))
numsum(nums,target)