给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):
var fourSum = function (nums, target) {
if (nums.length < 4) return []
let res = []
nums.sort((a, b) => a - b)
for (let i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue
}
for (let j = i + 1; j < nums.length; j++) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue
}
for (let k = j + 1; k < nums.length; k++) {
if (k > j + 1 && nums[k] == nums[k - 1]) {
continue
}
for (let l = k + 1; l < nums.length; l++) {
if (l > k + 1 && nums[l] == nums[l - 1]) {
continue
}
if (nums[i] + nums[j] + nums[k] + nums[l] == target) {
res.push([nums[i], nums[j], nums[k], nums[l]])
}
}
}
}
}
var demo = [...new Set(res.map((i) => JSON.stringify(i)))].map((i) => JSON.parse(i))
return demo
};
nums = [0, 0, 0, 0, 0], target = 0
// console.log(fourSum(nums, target))