Leetcode 18.4Sum

本文探讨了在给定整数数组中寻找四个元素之和等于特定目标值的所有唯一组合的问题。基于三数之和的基础上增加了额外一层循环,并通过剪枝技术减少不必要的计算,最终实现时间复杂度为O(N^3)的解决方案。

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

@author stormma
@date 2017/11/03


生命不息,奋斗不止!


题目

Given an array S of n integers, are there elements a, b, c, and d in S 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.

For example, given array S = [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]
]

思路分析

在3sum基础上,再加一次循环即可,可以增加剪枝来换取运行时间,
时间复杂度O(N^3)

代码实现


class Solution {
        public List<List<Integer>> fourSum(int[] nums, int target) {
            List<List<Integer>> ans = new ArrayList<>();
            if (nums.length < 4) {
                return ans;
            }
            Arrays.sort(nums);
            if (4 * nums[0] > target || 4 * nums[nums.length - 1] < target) {
                return ans;
            }
            int max = nums[nums.length - 1];
            // -2 -1 0 0 1 2
            for (int i = 0; i < nums.length - 3; i++) {
                if (i > 0 && nums[i] == nums[i - 1]) {
                    continue;
                }
                if (4 * nums[i] > target) {
                    break;
                }
                // 剪枝
                if (nums[i] + 3 * max < target) {
                    continue;
                }
                for (int j = i + 1; j < nums.length - 2; j++) {
                    // 3
                    // -4 -3 0 5 5 10
                    int low = j + 1, high = nums.length - 1, goal = target - nums[i] - nums[j];
                    // 剪枝
                    if (nums[i] + nums[j] + 2 * max < target) {
                        continue;
                    }
                    // 去重
                    if (j > i + 1 && nums[j - 1] == nums[j]) {
                        continue;
                    }
                    while (low < high) {
                        if (nums[low] + nums[high] < goal) {
                            low++;
                        } else if (nums[low] + nums[high] > goal) {
                            high--;
                        } else {
                            ans.add(Arrays.asList(nums[i], nums[j], nums[low], nums[high]));
                            while (low < high && nums[low + 1] == nums[low]) {
                                low++;
                            }
                            while (low < high && nums[high - 1] == nums[high]) {
                                high--;
                            }
                            low++;
                            high--;
                        }
                    }
                }
            }
            return ans;
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值