-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4Sum.java
More file actions
65 lines (50 loc) · 1.77 KB
/
4Sum.java
File metadata and controls
65 lines (50 loc) · 1.77 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
import java.util.*;
class Solution{
public List<List<Integer>> fourSum(int[] nums, int target){
Map<Long,Integer>numMap = new HashMap<>();
List<List<Integer>>listOfList = new ArrayList<>();
Set<List<Integer>>setOfList = new HashSet<>();
int n = nums.length;
for (int i = 0; i < n; i++) {
numMap.put((long)nums[i],i);
}
int i = 0;
long check;
while(i<n-2){
int j = i+1;
while(j<n-1){
int k=j+1;
while (k<n){
long longValue = (long)nums[i]+(long)nums[j]+(long)nums[k];
check = (target-longValue);
if(numMap.containsKey(check) && numMap.get(check)!=i && numMap.get(check)!=j && numMap.get(check)!= k){
List<Integer> list = new ArrayList<>();
int[] intValue = new int[4];
intValue[0]=nums[i];
intValue[1]=nums[j];
intValue[2]=nums[k];
intValue[3]=nums[numMap.get(check)];
Arrays.sort(intValue);
list.add(intValue[0]);
list.add(intValue[1]);
list.add(intValue[2]);
list.add(intValue[3]);
setOfList.add(list);
}
k++;
}
j++;
}
i++;
}
listOfList.addAll(setOfList);
return listOfList;
}
}
public class LeetCode {
public static void main(String[] args) {
Solution solution = new Solution();
int[] a = {1,0,-1,0,-2,2};
System.out.println(solution.fourSum(a,0));
}
}