集册 LeetCode 题解 18.4 Sum(4 个数的和)

18.4 Sum(4 个数的和)

—— 4 Sum(4 个数的和)

欢马劈雪     最近更新时间:2020-08-04 05:37:59

154

翻译

给定一个有 n 个数字的数组 S,在 S 中是否存在元素 a,b,c 和 d 的和恰好满足 a + b + c + d = target。

找出数组中所有的不想等的这四个元素,其和等于 target。

备注:

在(a,b,c,d)中的元素必须从小到大排列。(a ≤ b ≤ c ≤ d)
其结果必须不能够重复。

例如,给定 S = {1 0 -1 0 -2 2},target = 0。
一个结果集为:
(-1, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)

原文

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. (ie, a ≤ b ≤ c ≤ d)
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)

代码

具体的方法和前面两道题一样,我就不再赘述了。

展开阅读全文