给定一个长度是 n 的数组 nums ,和一个目标值 target,请你找出不重复的四元组 [nums[a],nums[b],nums[c],nums[d]] (如果四元组的元素一一对应则只输出其中一组)
同时四元组要满足 各不相同,
你可以按任意顺序输出
数据范围: ,
[2,0,-2,3,-3,0],0
[[2,-2,0,0],[3,-3,0,0],[2,-2,3,-3]]
function fournumber( nums , target ) { // write code here nums.sort((a, b) => a-b); let res = []; let max = nums.length-1; let i = 0; let num1 = nums[i]; while(i < nums.length - 3) { let i2 = i + 1; let l = i2 + 1; let h = max; while(i2 < nums.length - 2){ let num2 = nums[i2]; let low = nums[l]; let high = nums[h]; while(l < h) { if(num1 + num2 + low + high == target) { res.push([num1, num2, low, high]); while(nums[l] == low) {l++;} while(nums[h] == high) {h--;} low = nums[l]; high = nums[h]; } else if (num1 + num2 + low + high < target) { while(nums[l] == low) {l++;} low = nums[l]; } else if (num1 + num2 + low + high > target) { while(nums[h] == high) {h--;} high = nums[h]; } } while(nums[i2] == num2) {i2++;} l = i2 + 1; h = max; } while(num1 == nums[i]) {i++;} num1 = nums[i]; } return res; }