Array-4Sum问题

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 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]
]
 1 //解题思路:将之前解决的3Sum问题再加一层循环,注意两层for循环中的条件
 2 public class Solution {
 3     public List<List<Integer>> fourSum(int[] nums, int target) {
 4        List<List<Integer>> result = new LinkedList<>();
 5        if(nums == null || nums.length < 4) {
 6            return result;
 7        }
 8        Arrays.sort(nums);//注意一般情况都是要先进行排序的
 9        for(int first = 0; first < nums.length - 3; first++) {
10            if(first > 0 && nums[first] == nums[first - 1]) {
11                continue;
12            }
13            for(int second = first + 1; second < nums.length - 2; second++) {
14                if(second > first + 1 && nums[second] == nums[second - 1]) {//这里要注意的是 second > first + 1
15                    continue;
16                }
17                int left = second + 1; int right = nums.length - 1; int target1 = target - (nums[first] + nums[second]);
18                while(left < right) {
19                    int sum = nums[left] + nums[right];
20                    if(sum == target1) {
21                        result.add(Arrays.asList(nums[first], nums[second], nums[left], nums[right]));
22                        while(left < right && nums[left] == nums[left + 1]) left++;
23                        while(left < right && nums[right] == nums[right - 1]) right--;
24                        left++;
25                        right--;
26                    } else if(sum < target1) {
27                        left++;
28                    } else {
29                        right--;
30                    }
31                }
32            }
33        }
34        return result;
35     }
36 }
时间: 2024-10-10 09:14:54

Array-4Sum问题的相关文章

LeetCode[Array]---- 4Sum

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 o

Array + two points leetcode.18 - 4Sum

题面 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. 给定无序数组,找到和为target的不重复的长度为4的子序列 样例 1. Given

[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 017 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 o

[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

18 &amp; 454. 4Sum I &amp; II

18. 4Sum I (32lines) Given a integer array and target, find all unique quadruplets sum is target. Solution(O(N^3)): Similar as 3sum problem, add one more loop and be careful about the duplicates. 1 public class Solution { 2 public List<List<Integer&

【Leetcode】4Sum

题目链接:https://leetcode.com/problems/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 quadrupl

[LeetCode][JavaScript]4Sum

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 o

1. Two Sum&amp;&amp;15. 3Sum&amp;&amp;18. 4Sum

题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Given nums = [2, 7, 1

3Sum &amp; 4Sum

3 Sum Given an array S of n integers, are there elements a, b, c in Ssuch that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Notice Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The