Given an array nums of n integers, return an array of all the unique quadruplets [nums[a], nums[b], nums[c], nums[d]] such that:
Example 1:
Example 2:
Constraints:
四数之和。
给你一个由 n 个整数组成的数组 nums ,和一个目标值 target 。请你找出并返回满足下述全部条件且不重复的四元组 [nums[a], nums[b], nums[c], nums[d]] (若两个四元组元素一一对应,则认为两个四元组重复):https://leetcode.cn/problems/4sum 
 
思路 依然是双指针逼近的思路做,注意以下几点
需要对 input 排序 
需要四个指针 - i, j, low, high 
每个指针都需要跳过重复元素 
i 最多到 nums.length - 3 
 
复杂度 时间O(n^3)
代码 Java实现 - 可以处理整型溢出的问题(19行) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 class  Solution  {public  List<List<Integer>> fourSum (int [] nums, int  target)  {new  ArrayList <>();if  (nums.length < 4 ) {return  res;for  (int  i  =  0 ; i < nums.length - 3 ; i++) {if  (i > 0  && nums[i] == nums[i - 1 ]) {continue ;for  (int  j  =  i + 1 ; j < nums.length - 2 ; j++) {if  (j > i + 1  && nums[j] == nums[j - 1 ]) {continue ;int  low  =  j + 1 ;int  hi  =  nums.length - 1 ;while  (low < hi) {long  sum  =  (long ) target - nums[i] - nums[j];if  (nums[low] + nums[hi] == sum) {while  (low < hi && nums[low] == nums[low + 1 ]) {while  (low < hi && nums[hi] == nums[hi - 1 ]) {else  if  (nums[low] + nums[hi] < sum) {else  {return  res;
JavaScript实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 var  fourSum = function (nums, target ) {sort ((a, b ) =>  a - b);const  res = [];let  low, high, sum;if  (nums.length  < 4 ) return  res;for  (let  i = 0 ; i < nums.length  - 3 ; i++) {if  (i > 0  && nums[i] === nums[i - 1 ]) continue ;for  (let  j = i + 1 ; j < nums.length  - 2 ; j++) {if  (j > i + 1  && nums[j] === nums[j - 1 ]) continue ;1 ;length  - 1 ;while  (low < high) {if  (sum === target) {push ([nums[i], nums[j], nums[low], nums[high]]);while  (low < high && nums[low] === nums[low + 1 ]) low++;while  (low < high && nums[high] === nums[high - 1 ]) high--;else  if  (sum < target) {else  {return  res;