LeetCode:四数之和【18】

LeetCode:四数之和【18】

题目描述

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。

注意:

  答案中不可以包含重复的四元组。

示例:

  给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。

满足要求的四元组集合为:
[
  [-1, 0, 0, 1],
  [-2, -1, 1, 2],
  [-2, 0, 0, 2]
]

题目分析

  四数之和和三数之和求解思路一毛一样。三数之和解析:https://www.cnblogs.com/MrSaver/p/5913336.html

  首先数组进行有序话,接着我们固定a和b,在ab固定的情况下,cd分别向内考虑,调整四数之和

  当c和d把所有情况都遍历完后,b向右移动一格并固定,cd分别向内考虑,调整四数之和

  当b移动到倒数第三位时,b的所有情况就算遍历玩了,最后a向右移动一格并固定

  

  我们知道用双指针模型解决问题,就要将N数之和的前N-2项进行固定,通过移动N项和N-1项来匹配目标值,即五数之和就需要将a,b,c固定,移动d、e

  所谓固定不是不移动,他们仍然需要进行暴力的遍历操作,只是在双指针移动完后再动。

  这道题还是很值得思考的!

Java题解

public class Solution {
    public static List<List<Integer>> fourSum(int[] nums, int target) {
        //[!]这里引入map用来去重
        Map<String,Boolean> map = new HashMap<>();
        List<List<Integer>> result = new ArrayList<>();

        //[!]数组长度小于4直接返回空
        if(nums.length < 4) return result;

        //[!]做排序以便使用双指针模型
        Arrays.sort(nums);

        int a = 0;
        while(a < nums.length - 3) {
            int b = a + 1;
            while (b<nums.length-2){
                int c = b + 1;
                int d = nums.length-1;
                while(c < d) {
                    int sum = nums[a] + nums[b] + nums[c]+nums[d];
                    if(sum == target) {
                        if(!map.containsKey(nums[a]+"|"+nums[b]+"|"+nums[c]+"|"+nums[d]))
                        {
                            map.put(nums[a]+"|"+nums[b]+"|"+nums[c]+"|"+nums[d],true);
                            result.add(Arrays.asList(nums[a], nums[b], nums[c],nums[d]));
                        }
                    }
                    //[!]一直递增C,直到和等于或大于目标值
                    if(sum <= target) while(nums[c] == nums[++c] && c < d);
                    //[!]当值大于目标值时,开始递减D,等于时判断使d跳过重复值
                    if(sum >= target) while(nums[d--] == nums[d] && c < d);
                }
                b++;
            }
            a++;
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {1,0,-1,0,-2,2};
        fourSum(nums,0);

    }
}

  

  

原文地址:https://www.cnblogs.com/MrSaver/p/11613102.html

时间: 2024-08-27 18:13:16

LeetCode:四数之和【18】的相关文章

Leetcode(18)-四数之和

给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 注意: 答案中不可以包含重复的四元组. 示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0. 满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ] 思路:

[LeetCode] 454. 4Sum II 四数之和II

Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero. To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the r

LeetCode第十八题-四数之和

4Sum 问题简介:定n个整数和整数目标的数组nums,是否有元素a,b,c,d在nums中,使a+b+c+d=target? 举例: 给定数组 nums = [1, 0, -1, 0, -2, 2], 目标值 target = 0. 结果: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ] 解法一:先将数组排序,通过三层循环,寻找是否符合四数之和的结果 注: 1.contains方法使用场景 list/Set - contains() Map -

两数之和,三数之和,最接近的三数之和,四数之和

LeetCode有一系列做法套路相同的题目,N数之和就可以算一个 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 第一个解决办法,简单暴力,堆for循环就是,但

LeetCode 18. 4Sum (四数之和)

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 exampl

LeetCode(18):四数之和

Medium! 题目描述: 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 注意: 答案中不可以包含重复的四元组. 示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0. 满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0

LeetCode 18. 四数之和(4Sum)

题目描述 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组. 注意: 答案中不可以包含重复的四元组. 示例: 给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0. 满足要求的四元组集合为: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]

[Leetcode 18]四数之和 4 Sum

[题目] Given an array nums of n integers and an integer target, are there elements a, b, c, 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 d

LeetCode 18. 四数之和

题意 从数组中找出满足和为target的四元组. 思路 想法1:暴力,\(O(n^4)\). 想法2:排序 + 指针.思路类似前面的三数之和,只不过这里的指针多了一个,后两个指针相遇时第二个指针后移一个单位.时间复杂度:\(O(n^3)\),空间复杂度:\(O(1)\). 代码 class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { int len