码迷,mamicode.com
首页 > 其他好文 > 详细

3Sum & 4Sum

时间:2016-07-04 07:41:52      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

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.

4Sum

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

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5639346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!