[Leetcode 18]四数之和 4 Sum

【题目】

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.

【思路】

和leetcode15三数之和3 Sum类似https://www.cnblogs.com/inku/p/9955638.html

多一次循环,加粗部分是新增的

重点是去重。

【代码】

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> data=new ArrayList<>();
        Arrays.sort(nums);
        for(int i=0;i<nums.length-2;i++){
            for(int j=nums.length-1;j>0;j--){
                int left=i+1;
                int right=j-1;
                if(i>0&&nums[i]==nums[i-1]){
                    continue;}
                if(j<nums.length-1&&nums[j]==nums[j+1]){
                    continue;}
                while(left<right){
                    int sum=nums[left]+nums[right]+nums[i]+nums[j];
                    if(sum==target){
                        data.add(Arrays.asList(nums[left], nums[right],nums[i],nums[j]));
                        left++;
                        right--;
                        while(left<right&&nums[left]==nums[left-1])
                            left++;
                        while(left<right&&nums[right]==nums[right+1])
                            right--;
                    }
                    else if(left<right&&sum>target)
                        right--;
                    else
                        left++;
                }
            }
        }
        return data;
    }
}

原文地址:https://www.cnblogs.com/inku/p/9977917.html

时间: 2024-08-04 17:46:36

[Leetcode 18]四数之和 4 Sum的相关文章

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. 四数之和

题意 从数组中找出满足和为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

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]

[LeetCode] 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: Elements in a quadruplet (a,b,c,d) must be in non-descending order.

[Leetcode 15]三数之和 3 Sum

[题目] Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: The solution set must not contain duplicate triplets. Example: Given array nu

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 1. 两数之和(Two Sum)

目录 题目描述: 示例: 解法: 题目描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 解法: class Solution { public: vector tw

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