双指针 - 四数组问题[M]

优质
小牛编辑
140浏览
2023-12-01

018. 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 ≤ 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, 0, 0, 1)
(-2, -1, 1, 2)
(-2, 0, 0, 2)

思路

好了,2SUM,3SUM,3SUM Closet 都解决了,我们顺利迎来了最后一个boss,4SUM,但是我们已经见怪不怪了。老思路,把4SUM问题变成3SUM问题。

  • 先排序
  • 确定一个数,然后文件顺利变成3SUM问题,然后就是完完全全3SUM的解决思路了。
  1. for(int i = 0;i < nums.length-3;i++)
  2. {
  3. int target_i = target - nums[i];
  4. if(i > 0 && nums[i] == nums[i-1]) //排除一样的数
  5. continue;
  6. ……//3SUM问题
  7. }

整体代码

  1. public class Solution {
  2. public List<List<Integer>> fourSum(int[] nums, int target) {
  3. Arrays.sort(nums);
  4. List<List<Integer>> list;
  5. list = new ArrayList<List<Integer>>();
  6. int mid,right;
  7. for(int i = 0;i < nums.length-3;i++)
  8. {
  9. int target_i = target - nums[i];
  10. if(i > 0 && nums[i] == nums[i-1])
  11. continue;
  12. for (int left = i+1; left < nums.length-2; left++)
  13. {
  14. mid = left+1;
  15. right = nums.length-1;
  16. int tmp = target_i-nums[left];
  17. if(left > i+1 && nums[left] == nums[left-1])
  18. continue;
  19. while(mid < right)
  20. {
  21. if(nums[mid] + nums[right] == tmp)
  22. {
  23. int tmp_mid = nums[mid],tmp_right= nums[right];
  24. list.add(Arrays.asList(nums[i],nums[left], nums[mid], nums[right]));
  25. while(mid < right && nums[++mid] == tmp_mid);
  26. while(mid < right && nums[--right] == tmp_right);
  27. }
  28. else if(nums[mid] + nums[right] < tmp)
  29. mid++;
  30. else
  31. right--;
  32. }
  33. }
  34. }
  35. return list;
  36. }
  37. }

思路2:排除不可能情况

讨论区rikimberley有个高分的答案,具体思路还是和上面的思路一样的,但是它的运行速度超过了100%的人,是因为它在运行的时候,排除了很多不可能的情况。假设我们考虑4个数分别为A B C D(有序),最大值MAX。

  1. A太大,退出:(如果4*A > target)
  2. A太小,跳过:(A+4*MAx < target)
  3. 确定A后求BCD的3SUM问题
  4. B太大,退出:(如果3*B > target)
  5. B太小,跳过:(B+3*MAx < target)
    ……
  1. public List<List<Integer>> fourSum(int[] nums, int target) {
  2. ArrayList<List<Integer>> res = new ArrayList<List<Integer>>();
  3. int len = nums.length;
  4. if (nums == null || len < 4)
  5. return res;
  6. Arrays.sort(nums);
  7. int max = nums[len - 1];
  8. if (4 * nums[0] > target || 4 * max < target)
  9. return res;
  10. int i, z;
  11. for (i = 0; i < len; i++) {
  12. z = nums[i];
  13. if (i > 0 && z == nums[i - 1])// avoid duplicate
  14. continue;
  15. if (z + 3 * max < target) // z is too small
  16. continue;
  17. if (4 * z > target) // z is too large
  18. break;
  19. if (4 * z == target) { // z is the boundary
  20. if (i + 3 < len && nums[i + 3] == z)
  21. res.add(Arrays.asList(z, z, z, z));
  22. break;
  23. }
  24. threeSumForFourSum(nums, target - z, i + 1, len - 1, res, z);
  25. }
  26. return res;
  27. }
  28. /*
  29. * Find all possible distinguished three numbers adding up to the target
  30. * in sorted array nums[] between indices low and high. If there are,
  31. * add all of them into the ArrayList fourSumList, using
  32. * fourSumList.add(Arrays.asList(z1, the three numbers))
  33. */
  34. public void threeSumForFourSum(int[] nums, int target, int low, int high, ArrayList<List<Integer>> fourSumList,
  35. int z1) {
  36. if (low + 1 >= high)
  37. return;
  38. int max = nums[high];
  39. if (3 * nums[low] > target || 3 * max < target)
  40. return;
  41. int i, z;
  42. for (i = low; i < high - 1; i++) {
  43. z = nums[i];
  44. if (i > low && z == nums[i - 1]) // avoid duplicate
  45. continue;
  46. if (z + 2 * max < target) // z is too small
  47. continue;
  48. if (3 * z > target) // z is too large
  49. break;
  50. if (3 * z == target) { // z is the boundary
  51. if (i + 1 < high && nums[i + 2] == z)
  52. fourSumList.add(Arrays.asList(z1, z, z, z));
  53. break;
  54. }
  55. twoSumForFourSum(nums, target - z, i + 1, high, fourSumList, z1, z);
  56. }
  57. }
  58. /*
  59. * Find all possible distinguished two numbers adding up to the target
  60. * in sorted array nums[] between indices low and high. If there are,
  61. * add all of them into the ArrayList fourSumList, using
  62. * fourSumList.add(Arrays.asList(z1, z2, the two numbers))
  63. */
  64. public void twoSumForFourSum(int[] nums, int target, int low, int high, ArrayList<List<Integer>> fourSumList,
  65. int z1, int z2) {
  66. if (low >= high)
  67. return;
  68. if (2 * nums[low] > target || 2 * nums[high] < target)
  69. return;
  70. int i = low, j = high, sum, x;
  71. while (i < j) {
  72. sum = nums[i] + nums[j];
  73. if (sum == target) {
  74. fourSumList.add(Arrays.asList(z1, z2, nums[i], nums[j]));
  75. x = nums[i];
  76. while (++i < j && x == nums[i]) // avoid duplicate
  77. ;
  78. x = nums[j];
  79. while (i < --j && x == nums[j]) // avoid duplicate
  80. ;
  81. }
  82. if (sum < target)
  83. i++;
  84. if (sum > target)
  85. j--;
  86. }
  87. return;
  88. }