4Sum

描述

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≤da \leq b \leq c \leq da≤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. (-1, 0, 0, 1)
  2. (-2, -1, 1, 2)
  3. (-2, 0, 0, 2)

分析

先排序,然后左右夹逼,复杂度 O(n3)O(n^3),会超时。

可以用一个hashmap先缓存两个数的和,最终复杂度O(n3)O(n^3)。这个策略也适用于 3Sum 。

左右夹逼

  1. // 4Sum
  2. // 先排序,然后左右夹逼
  3. // Time Complexity: O(n^3),Space Complexity: O(1)
  4. public class Solution {
  5. public List<List<Integer>> fourSum(int[] nums, int target) {
  6. List<List<Integer>> result = new ArrayList<>();
  7. if (nums.length < 4) return result;
  8. Arrays.sort(nums);
  9. for (int i = 0; i < nums.length - 3; ++i) {
  10. if (i > 0 && nums[i] == nums[i-1]) continue;
  11. for (int j = i + 1; j < nums.length - 2; ++j) {
  12. if (j > i+1 && nums[j] == nums[j-1]) continue;
  13. int k = j + 1;
  14. int l = nums.length - 1;
  15. while (k < l) {
  16. final int sum = nums[i] + nums[j] + nums[k] + nums[l];
  17. if (sum < target) {
  18. ++k;
  19. while(nums[k] == nums[k-1] && k < l) ++k;
  20. } else if (sum > target) {
  21. --l;
  22. while(nums[l] == nums[l+1] && k < l) --l;
  23. } else {
  24. result.add(Arrays.asList(nums[i], nums[j], nums[k], nums[l]));
  25. ++k;
  26. --l;
  27. while(nums[k] == nums[k-1] && k < l) ++k;
  28. while(nums[l] == nums[l+1] && k < l) --l;
  29. }
  30. }
  31. }
  32. }
  33. return result;
  34. }
  35. }

HashMap 做缓存

  1. // 4Sum
  2. // 先排序,然后左右夹逼
  3. // Time Complexity: O(n^3),Space Complexity: O(1)
  4. public class Solution {
  5. public List<List<Integer>> fourSum(int[] nums, int target) {
  6. List<List<Integer>> result = new ArrayList<>();
  7. if (nums.length < 4) return result;
  8. Arrays.sort(nums);
  9. final HashMap<Integer, ArrayList<int[]>> cache = new HashMap<>();
  10. for (int i = 0; i < nums.length; ++i) {
  11. for (int j = i + 1; j < nums.length; ++j) {
  12. ArrayList<int[]> value = cache.get(nums[i] + nums[j]);
  13. if (value == null) {
  14. value = new ArrayList<>();
  15. cache.put(nums[i] + nums[j], value);
  16. }
  17. value.add(new int[]{i, j});
  18. }
  19. }
  20. final HashSet<String> used = new HashSet<>(); // avoid duplicates
  21. for (int i = 0; i < nums.length; ++i) {
  22. if (i > 0 && nums[i] == nums[i-1]) continue;
  23. for (int j = i + 1; j < nums.length - 2; ++j) {
  24. if (j > i+1 && nums[j] == nums[j-1]) continue;
  25. final ArrayList<int[]> list = cache.get(target - nums[i] - nums[j]);
  26. if (list == null) continue;;
  27. for (int[] pair : list) {
  28. if (j >= pair[0]) continue; // overlap
  29. final Integer[] sol = new Integer[]{nums[i], nums[j], nums[pair[0]], nums[pair[1]]};
  30. Arrays.sort(sol);
  31. final String key = Arrays.toString(sol);
  32. if(!used.contains(key)){
  33. result.add(Arrays.asList(sol));
  34. used.add(key);
  35. }
  36. }
  37. }
  38. }
  39. return result;
  40. }
  41. }

相关题目

原文: https://soulmachine.gitbooks.io/algorithm-essentials/content/java/linear-list/array/4sum.html