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

18 & 454. 4Sum I & II

时间:2017-01-11 08:12:28      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:let   list   amp   blog   while   sort   one   code   lin   

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

标签:let   list   amp   blog   while   sort   one   code   lin   

原文地址:http://www.cnblogs.com/CornerCase/p/6272215.html

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