《中英双解》leetcode 4Sum

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:

0 <= a, b, c, d < n
a, b, c, and d are distinct.
nums[a] + nums[b] + nums[c] + nums[d] == target
You may return the answer in any order.

给你一个由 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
你可以按 任意顺序 返回答案 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/4sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

Example 1:

Input: nums = [1,0,-1,0,-2,2], target = 0
Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
Example 2:

Input: nums = [2,2,2,2,2], target = 8
Output: [[2,2,2,2]]
 

Constraints:

1 <= nums.length <= 200
-109 <= nums[i] <= 109
-109 <= target <= 109

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/4sum
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

package Sort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FourSum {
    public static int len = 0;
   public static List<List<Integer>> forSum(int[] nums,int target){
       len = nums.length;
       Arrays.sort(nums);
       return KSum(nums,target,4,0);
   }
   public static ArrayList<List<Integer>> KSum(int[] nums,int target,int k,int index){
       ArrayList<List<Integer>> res = new ArrayList<>();
       if(index >= len){
           return res;
       }
       if(k == 2){
           int i = index,j = len - 1;
           while (i < j){
               //find a pair
               if(target - nums[i] == nums[j]) {
                   List<Integer> temp = new ArrayList<>();
                   temp.add(nums[i]);
                   temp.add(target-nums[i]);
                   res.add(temp);
                   //skip duplication
                   while(i<j && nums[i]==nums[i+1]) i++;
                   while(i<j && nums[j-1]==nums[j]) j--;
                   i++;
                   j--;
                   //move left bound
               } else if (target - nums[i] > nums[j]) {
                   i++;
                   //move right bound
               } else {
                   j--;
               }
           }
           } else {
           for (int i = index; i < len - k + 1; i++) {
               //use current number to reduce ksum into k-1sum
               ArrayList<List<Integer>> temp = KSum(nums, target - nums[i], k-1, i+1);
               if(temp != null){
                   //add previous results
                   for (List<Integer> t : temp) {
                       t.add(0, nums[i]);
                   }
                   res.addAll(temp);
               }
               while (i < len-1 && nums[i] == nums[i+1]) {
                   //skip duplicated numbers
                   i++;
               }
           }
       }
       return res;
   }

    public static void main(String[] args) {
        List<List<Integer>> lists = forSum(new int[]{-2,-1,0,0,1,2},0);
        for(List<Integer> list : lists){
            System.out.println(list);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值