Skip to content

Commit b1eac82

Browse files
author
zhangbo54
committed
18. 4Sum
1 parent 5b48382 commit b1eac82

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed

src/leetcode/_18_/Main.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package leetcode._18_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
// System.out.println(solution.fourSum(new int[]{-4, -1, -1, 0, 1, 2}, -1));
10+
// System.out.println(solution.fourSum(new int[]{-3, -2, -1, 0, 0, 1, 2, 3}, 0));
11+
// System.out.println(solution.fourSum(new int[]{-5, -4, -2, -2, -2, -1, 0, 0, 1}, -9));
12+
System.out.println(solution.fourSum(new int[]{-10, -10, -8, -7, -6, -5, -5, -2, -2, 0, 0, 1, 1, 5, 7, 7, 7, 7}, 28));
13+
// -2,-1,1,2
14+
15+
// Integer.parseInt("1" + Integer.MAX_VALUE);
16+
// -4 -1, -1, 0, 1, 2,
17+
}
18+
}
19+

src/leetcode/_18_/Solution.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package leetcode._18_;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collections;
6+
import java.util.HashSet;
7+
import java.util.List;
8+
import java.util.Set;
9+
10+
/**
11+
* Created by zhangbo54 on 2019-03-05.
12+
*/
13+
class Solution {
14+
public List<List<Integer>> fourSum(int[] nums, int target) {
15+
if (nums.length < 4) {
16+
return Collections.emptyList();
17+
}
18+
Arrays.sort(nums);
19+
Set<List<Integer>> result = new HashSet<>(); // 一开始写的是list,不过这个去重真的是很麻烦,还是使用set来去重了
20+
for (int i = 0; i < nums.length - 1; i++) {
21+
if (i > 0 && nums[i] == nums[i - 1]) {
22+
continue;
23+
}
24+
for (int j = i + 1; j < nums.length; j++) {
25+
int targetSum = target - (nums[i] + nums[j]);
26+
if (targetSum < nums[j] && nums[j] > 0) { // 因为已经排序了,所以后面的任意两个数和必然大于 targetSum
27+
break;
28+
}
29+
for (int x = j + 1, y = nums.length - 1; x < y; ) {
30+
if (nums[x] + nums[y] > targetSum) {
31+
y--;
32+
} else if (nums[x] + nums[y] < targetSum) {
33+
x++;
34+
} else {
35+
result.add(Arrays.asList(nums[i], nums[j], nums[x], nums[y]));
36+
x++;
37+
y--;
38+
39+
while (x < y && x >= 1 && nums[x] == nums[x - 1]) {
40+
x++;
41+
}
42+
while (x < y && y + 1 < nums.length && nums[y] == nums[y + 1]) {
43+
y--;
44+
}
45+
}
46+
}
47+
48+
}
49+
}
50+
return new ArrayList<>(result);
51+
}
52+
}
53+
54+
// [[-3,-2,2,3],[-3,-1,1,3],[-3,0,0,3],[-3,0,1,2],[-2,-1,0,3],[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]

src/leetcode/_18_/solution.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
### [18\. 4Sum](https://leetcode.com/problems/4sum/)
2+
3+
Difficulty: **Medium**
4+
5+
6+
Given an array `nums` of _n_ integers and an integer `target`, are there elements _a_, _b_, _c_, and _d_ in `nums` such that _a_ + _b_ + _c_ + _d_ = `target`? Find all unique quadruplets in the array which gives the sum of `target`.
7+
8+
**Note:**
9+
10+
The solution set must not contain duplicate quadruplets.
11+
12+
**Example:**
13+
14+
```
15+
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
16+
17+
A solution set is:
18+
[
19+
[-1, 0, 0, 1],
20+
[-2, -1, 1, 2],
21+
[-2, 0, 0, 2]
22+
]
23+
```
24+
25+
26+
#### Solution
27+
28+
Language: **Java**
29+
30+
```java
31+
class Solution {
32+
   public List<List<Integer>> fourSum(int[] nums, int target) {
33+
       if (nums.length < 4) {
34+
           return Collections.emptyList();
35+
      }
36+
       Arrays.sort(nums);
37+
       Set<List<Integer>> result = new HashSet<>(); // 一开始写的是list,不过这个去重真的是很麻烦,还是使用set来去重了
38+
       for (int i = 0; i < nums.length - 1; i++) {
39+
           if (i > 0 && nums[i] == nums[i - 1]) {
40+
               continue;
41+
          }
42+
           for (int j = i + 1; j < nums.length; j++) {
43+
               int targetSum = target - (nums[i] + nums[j]);
44+
               if (targetSum < nums[j] && nums[j] > 0) { // 因为已经排序了,所以后面的任意两个数和必然大于 targetSum
45+
                   break;
46+
              }
47+
               for (int x = j + 1, y = nums.length - 1; x < y; ) {
48+
                   if (nums[x] + nums[y] > targetSum) {
49+
                       y--;
50+
                  } else if (nums[x] + nums[y] < targetSum) {
51+
                       x++;
52+
                  } else {
53+
                       result.add(Arrays.asList(nums[i], nums[j], nums[x], nums[y]));
54+
                       x++;
55+
                       y--;
56+
57+
                       while (x < y && x >= 1 && nums[x] == nums[x - 1]) {
58+
                           x++;
59+
                      }
60+
                       while (x < y && y + 1 < nums.length && nums[y] == nums[y + 1]) {
61+
                           y--;
62+
                      }
63+
                  }
64+
              }
65+
66+
          }
67+
      }
68+
       return new ArrayList<>(result);
69+
  }
70+
}
71+
```
72+
![](https://ws1.sinaimg.cn/large/006tKfTcgy1g0sy56zmtdj30v90u0gq9.jpg)

0 commit comments

Comments
 (0)