-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFourSum.java
More file actions
67 lines (60 loc) · 2.61 KB
/
FourSum.java
File metadata and controls
67 lines (60 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.HashSet;
public class FourSum {
public static void main(String[] args) {
//Input: nums = [1,0,-1,0,-2,2], target = 0
//Output: [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
int[] ex1 = new int[]{1,0,-1,0,-2,2};
System.out.println(new FourSum().fourSum(ex1, 0));
//Input: nums = [2,2,2,2,2], target = 8
//Output: [[2,2,2,2]]
int[] ex2 = new int[]{2,2,2,2};
System.out.println(new FourSum().fourSum(ex2, 8));
int[] ex3 = new int[]{2,2,2,2, 2, 2, 2};
System.out.println(new FourSum().fourSum(ex3, 8));
int[] ex4 = new int[]{-5,-4,-3,-2,-1,0,0,1,2,3,4,5};
System.out.println(new FourSum().fourSum(ex4, 0));
int[] ex5 = new int[]{1000000000,1000000000,1000000000,1000000000};
System.out.println(new FourSum().fourSum(ex5, -294967296));
}
public List<List<Integer>> fourSum(int[] nums, int target) {
Arrays.sort(nums);
List<List<Integer>> sol = new ArrayList<List<Integer>>();
ArrayList<Integer> solution = new ArrayList<Integer>();
HashSet<ArrayList<Integer>> solutionSet = new HashSet<ArrayList<Integer>>();
long sum = 0;
for (int i = 0; i < nums.length-3; i++) {
for (int j = i+1; j < nums.length-2; j++) {
int left = j + 1;
int right = nums.length - 1;
while (left < right) {
sum = nums[i];
sum += nums[j];
sum += nums[left];
sum += nums[right];
if (sum == target) {
// check that solution is different
solution = new ArrayList<Integer>();
solution.add(nums[i]);
solution.add(nums[j]);
solution.add(nums[left]);
solution.add(nums[right]);
if (!solutionSet.contains(solution)) {
sol.add(solution);
solutionSet.add(solution);
}
left++;
right--;
}
else if (sum < target)
left++;
else
right--;
} // end while
} // end for
} // end end for
return sol;
} // end mehtod
}