LeetCode18---fourSum(可扩展自ksum)

博客围绕 LeetCode 184sum 问题展开,给定含 n 个整数的数组和一个整数,需找出数组中所有满足 a + b + c + d 等于该整数的唯一四元组。提到可用回溯求解,但运行时间长会报错 LTE,也可作为一种思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

LeetCode--184sum

Given an array nums of n integers and an integer target, are there elements abc, and d in nums 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.

Example:

Given array nums = [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]
]

AC代码

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> ans = new ArrayList<>();
        helper(nums, target, res, ans, 0, 4);
        return res;
    }

    private void helper(int[] nums, int target, List<List<Integer>> res,
                             List<Integer> ans, int cur, int ksum) {
        if (nums.length - cur < ksum || ksum < 2  // 长度不够了
                || target < nums[cur] * ksum || target > nums[nums.length - 1] * ksum)  return;  // 不可能存在ksum元组

        if (ksum == 2) {
            helpTwoSum(nums, target, res, ans, cur);
        } else {
            for (int i = cur; i < nums.length; i++) {
                if (i > cur && nums[i - 1] == nums[i]) continue;  // 去重
                ans.add(nums[i]);
                helper(nums, target - nums[i], res, ans, i + 1, ksum - 1);
                ans.remove(ans.size() - 1);
            }
        }
    }

    private void helpTwoSum(int[] nums, int target, List<List<Integer>> res,
                                   List<Integer> ans, int cur) {
        int lo = cur;
        int hi = nums.length - 1;
        while (lo < hi) {
            int sum = nums[lo] + nums[hi];
            if (sum < target) {
                lo++;
            } else if (sum > target) {
                hi--;
            } else {
                List<Integer> tmp = new ArrayList<>(ans);
                tmp.add(nums[lo]); tmp.add(nums[hi]);
                res.add(tmp);
                lo++; hi--;
                while (lo < hi && nums[lo] == nums[lo - 1]) lo++;
                while (lo < hi && nums[hi] == nums[hi + 1]) hi--;
            }
        }
    }
}

本题还可以使用回溯来求解,但是运行时间较长,报错LTE了,但是也可以作为一种思路:

    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> res = new ArrayList<>();
        List<Integer> ans = new ArrayList<>();
        Arrays.sort(nums);
        help(nums, target, res, ans, 0, 4);
        return res;
    }

    private void help(int[] nums, int target, List<List<Integer>> res,
                      List<Integer> ans, int cur, int ksum) {
        if (ans.size() == ksum) {
            if (target == 0) res.add(new ArrayList<>(ans));
            return;
        }

        for (int i = cur; i < nums.length; ++i) {
            if (i > cur && nums[i] == nums[i - 1]) continue;  // 去重
            ans.add(nums[i]);
            help(nums, target - nums[i], res, ans, i + 1, ksum);
            ans.remove(ans.size() - 1);
        }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值