Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target?
Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
func fourSum(nums []int, target int) [][]int {
sort.Ints(nums)
var ret[][]int=make([][]int,0)
for i:=0;i<len(nums);i++{
if i>0&&nums[i]==nums[i-1]{
continue
}
for j:=i+1;j<len(nums);j++{
if j>i+1&&nums[j]==nums[j-1]{
continue
}
for k:=j+1;k<len(nums);k++{
if k>j+1&&nums[k]==nums[k-1]{
continue
}
for l:=k+1;l<len(nums);l++{
if l>k+1&&nums[l]==nums[l-1] {
continue
}
if nums[i]+nums[j]+nums[k]+nums[l]==target{
tmp:=make([]int,0)
tmp=append(tmp, nums[i])
tmp=append(tmp, nums[j])
tmp=append(tmp, nums[k])
tmp=append(tmp, nums[l])
ret=append(ret, tmp)
}
}
}
}
}
return ret
}