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]
]
借鉴三个数的方法,固定两个数,再用两个指针去搜寻。
public class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> res =

该博客探讨了LeetCode中第18题的解决方案,即寻找数组中四个数的组合,使它们的和等于目标值。文章提到了两种方法:固定两个数使用双指针搜索,或者固定一个数调用三数之和的算法。给出了具体的示例数组S和目标值target=0,并展示了可能的解集。
订阅专栏 解锁全文
1026

被折叠的 条评论
为什么被折叠?



