18 & 454. 4Sum I & II

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了18 & 454. 4Sum I & 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>> fourSum(int[] nums, int target) {
 3         Arrays.sort(nums);
 4         List<List<Integer>> result = new ArrayList<List<Integer>>();
 5         for(int i = 0; i < nums.length - 3; i++) {
 6             int sum3 = target - nums[i];
 7             for(int j = i+1; j < nums.length - 2; j++) {
 8                 int sum2 = sum3 - nums[j];
 9                 int lo = j+1, hi = nums.length - 1;
10                 while(lo < hi) {
11                     int sum = nums[i] + nums[j] + nums[lo] + nums[hi];
12                     if(sum == target) {
13                         List<Integer> item = new ArrayList<Integer>();
14                         item.add(nums[i]);
15                         item.add(nums[j]);
16                         item.add(nums[lo]);
17                         item.add(nums[hi]);
18                         result.add(item);
19                         while(lo < hi && nums[lo] == nums[lo+1]) lo++;
20                         while(lo < hi && nums[hi] == nums[hi-1]) hi--;
21                         lo++;hi--;
22                     }
23                     else if(sum > target) hi--;
24                     else lo++;
25                 }
26                 while(j < nums.length - 2 && nums[j] == nums[j + 1]) j++;
27             }
28             while(i < nums.length - 3 && nums[i] == nums[i + 1]) i++;
29         }
30         return result;
31     }
32 }

 

454. 4Sum II

Given 4 same-length arrays, find how many pairs of a[i] + b[j] + c[k] + d[l] = 0

Solution(O(N^2)):

Not same problem as the 2,3,4 sum. From different arrays, and can use hashmap, and make it a similar problem as the unordered 2 sum.

 1 ublic class Solution {
 2     public int fourSumCount(int[] A, int[] B, int[] C, int[] D) {
 3         int count = 0;
 4         int length = A.length;
 5         //sort is used and faster
 6         Arrays.sort(A);
 7         Arrays.sort(B);
 8         Arrays.sort(C);
 9         Arrays.sort(D);
10         HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
11         for(int i = 0; i < length; i++) {
12             for(int j = 0; j < length; j++) {
13                 int sum = A[i] + B[j];
14                 map.put(sum, map.getOrDefault(sum, 0) + 1);
15             }
16         }
17         
18         for(int k = 0; k < length; k++) {
19             for(int l = 0; l < length; l++) {
20                 int sum = C[k] + D[l];
21                 if(map.containsKey(-sum)) {
22                     count += map.get(-sum);
23                 }
24             }
25         }
26         return count;
27     }
28 }

 

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

454. 4Sum II

454. 4Sum II

454. 4Sum II

454. 4Sum II

454. 4Sum II

题目地址(4sum-ii/“>454. 四数相加 II)