LeetCode_18 4Sum

Given an array nums of n integers and an integer target, are there elements abc, and d in nums such that a + bc + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note:

The solution set must not contain duplicate quadruplets.

Example:

Given array nums = [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 List<List<Integer>> fourSum(int[] nums, int target) {
 2         List<List<Integer>> res = new ArrayList<>();
 3         Arrays.sort(nums);
 4         for (int i = 0; i < nums.length - 3; i++) {
 5             if (i != 0 && nums[i] == nums[i - 1])
 6                 continue;
 7             for (int j = i + 1; j < nums.length - 2; j++) {
 8                 if (j > i + 1 && nums[j] == nums[j - 1])
 9                     continue;
10                 int k = j + 1;
11                 int l = nums.length - 1;
12                 while (k < l) {
13                     int sum = nums[i] + nums[j] + nums[k] + nums[l];
14                     if (sum == target) {
15                         List<Integer> list = new ArrayList<>();
16                         list.add(nums[i]);
17                         list.add(nums[j]);
18                         list.add(nums[k]);
19                         list.add(nums[l]);
20                         res.add(list);
21                         k++;
22                         l--;
23                         // 去重复
24                         while (k < l && nums[k] == nums[k - 1]) {
25                             k++;
26                         }
27                         while (k < l && nums[l] == nums[l + 1]) {
28                             l--;
29                         }
30                     } else if (sum < target) {
31                         k++;
32                     } else {
33                         l--;
34                     }
35                 }
36             }
37         }
38         return res;
39     }
 1 public List<List<Integer>> fourSum3(int[] num, int target) {
 2         ArrayList<List<Integer>> ans = new ArrayList<>();
 3         if (num.length < 4)
 4             return ans;
 5         Arrays.sort(num);
 6         for (int i = 0; i < num.length - 3; i++) {
 7             if (num[i] + num[i + 1] + num[i + 2] + num[i + 3] > target)
 8                 break; // first candidate too large, search finished
 9             if (num[i] + num[num.length - 1] + num[num.length - 2] + num[num.length - 3] < target)
10                 continue; // first candidate too small
11             if (i > 0 && num[i] == num[i - 1])
12                 continue; // prevents duplicate result in ans list
13             for (int j = i + 1; j < num.length - 2; j++) {
14                 if (num[i] + num[j] + num[j + 1] + num[j + 2] > target)
15                     break; // second candidate too large
16                 if (num[i] + num[j] + num[num.length - 1] + num[num.length - 2] < target)
17                     continue; // second candidate too small
18                 if (j > i + 1 && num[j] == num[j - 1])
19                     continue; // prevents duplicate results in ans list
20                 int low = j + 1, high = num.length - 1;
21                 while (low < high) {
22                     int sum = num[i] + num[j] + num[low] + num[high];
23                     if (sum == target) {
24                         ans.add(Arrays.asList(num[i], num[j], num[low], num[high]));
25                         while (low < high && num[low] == num[low + 1])
26                             low++; // skipping over duplicate on low
27                         while (low < high && num[high] == num[high - 1])
28                             high--; // skipping over duplicate on high
29                         low++;
30                         high--;
31                     }
32                     // move window
33                     else if (sum < target)
34                         low++;
35                     else
36                         high--;
37                 }
38             }
39         }
40         return ans;
41     }

 

posted @ 2018-05-27 19:40  bwwbww  阅读(70997)  评论(0编辑  收藏  举报