File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ public List<List<Integer>> fourSum(int[] nums, int target) {
3
+ List<List<Integer>> rst = new ArrayList<List<Integer>>();
4
+ Arrays.sort(nums);
5
+
6
+ for (int i = 0; i < nums.length - 3; i++) {
7
+ if (i != 0 && nums[i] == nums[i - 1]) {
8
+ continue;
9
+ }
10
+
11
+ for (int j = i + 1; j < nums.length - 2; j++) {
12
+ if (j != i + 1 && nums[j] == nums[j - 1])
13
+ continue;
14
+
15
+ int left = j + 1;
16
+ int right = nums.length - 1;
17
+ while (left < right) {
18
+ int sum = nums[i] + nums[j] + nums[left] + nums[right];
19
+ if (sum < target) {
20
+ left++;
21
+ } else if (sum > target) {
22
+ right--;
23
+ } else {
24
+ ArrayList<Integer> tmp = new ArrayList<Integer>();
25
+ tmp.add(nums[i]);
26
+ tmp.add(nums[j]);
27
+ tmp.add(nums[left]);
28
+ tmp.add(nums[right]);
29
+ rst.add(tmp);
30
+ left++;
31
+ right--;
32
+ while (left < right && nums[left] == nums[left - 1]) {
33
+ left++;
34
+ }
35
+ while (left < right && nums[right] == nums[right + 1]) {
36
+ right--;
37
+ }
38
+ }
39
+ }
40
+ }
41
+ }
42
+
43
+ return rst;
44
+
45
+ }
46
+ }
You can’t perform that action at this time.
0 commit comments