3Sum & 4Sum

Posted 北叶青藤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了3Sum & 4Sum相关的知识,希望对你有一定的参考价值。

3 Sum

Given an array S of n integers, are there elements abc 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 solution set must not contain duplicate triplets.

Given an array S of n integers, are there elements abc, andd in S such that a + b + c + d = target?

Find all unique quadruplets in the array which gives the sum of target.

 Notice

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.

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 public class Solution {
 2     /**
 3      * @param numbers : Give an array numbersbers of n integer
 4      * @param target : you need to find four elements that\'s sum of target
 5      * @return : Find all unique quadruplets in the array which gives the sum of
 6      *           zero.
 7      */
 8     public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
 9         ArrayList<ArrayList<Integer>> rst = new ArrayList<ArrayList<Integer>>();
10         if(num == null || num.length < 4) {
11             return rst;
12         }
13         Arrays.sort(num);
14         for (int i = 0; i <= num.length - 4; i++) {
15             if (i != 0 && num[i] == num[i - 1]) {
16                 continue; // to skip duplicate numbers; e.g [0,0,0,0]
17             }
18             for (int j = i + 1; j <= num.length - 3; j++) {
19                 if (j != i + 1 && num[j] == num[j - 1]) {
20                     continue; // to skip duplicate numbers; e.g [0,0,0,0]
21                 }
22                 int left = j + 1;
23                 int right = num.length - 1;
24                 while (left < right) {
25                     int sum = num[left] + num[right] + num[i] + num[j] - target;
26                     if (sum == 0) {
27                         ArrayList<Integer> tmp = new ArrayList<Integer>();
28                         tmp.add(num[i]);
29                         tmp.add(num[j]);
30                         tmp.add(num[left]);
31                         tmp.add(num[right]);
32                         rst.add(tmp);
33                         left++;
34                         right--;
35                         while (left < right && num[left] == num[left - 1]) { // to skip duplicates
36                             left++;
37                         }
38                         while (left < right && num[right] == num[right + 1]) { // to skip duplicates
39                             right--;
40                         }
41                     } else if (sum < 0) {
42                         left++;
43                     } else {
44                         right--;
45                     }
46                 }
47             }
48 
49         }
50         return rst;
51     }
52 }

转载请注明出处: cnblogs.com/beiyeqingteng/

以上是关于3Sum & 4Sum的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode——4Sum &amp; 总结

4Sum -- LeetCode

3Sum and 4Sum Problems

4Sum - LeetCode

LeetCode——18. 4Sum

Leetcode18 4Sum