Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
58 lines (54 loc) · 2.39 KB

File metadata and controls

58 lines (54 loc) · 2.39 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.util.*;
public class Solution {
public List<List<Integer>> fourSum(int[] num, int target) {
Arrays.sort(num);
Map <Integer, List<List<Integer>>> map = new HashMap<Integer, List<List<Integer>>>();
List<List<Integer>> result = new ArrayList<List<Integer>>();
Set<List<Integer>> result_set = new HashSet<List<Integer>>();
for (int i = 0; i < num.length-1; i++) {
for (int j = i+1; j < num.length; j++) {
List<Integer> l = new ArrayList<Integer>();
l.add(i);
l.add(j);
int key = num[i] + num[j];
if (map.containsKey(key) == false) {
List<List<Integer>> curr = new ArrayList<List<Integer>>();
curr.add(l);
map.put(key, curr);
} else {
List<List<Integer>> curr = map.get(key);
curr.add(l);
map.put(key, curr);
}
}
}
for (Map.Entry<Integer, List<List<Integer>>> entry : map.entrySet()) {
if (map.containsKey(target-entry.getKey())) {
List<List<Integer>> e = map.get(entry.getKey());
List<List<Integer>> t_e = map.get(target-entry.getKey());
for (int i = 0; i < e.size(); i++) {
for (int j = 0; j < t_e.size(); j++) {
if (e.get(i).get(1) < t_e.get(j).get(0) || e.get(i).get(0) > t_e.get(j).get(1)) {
int[] indices = {e.get(i).get(1), e.get(i).get(0), t_e.get(j).get(0), t_e.get(j).get(1)};
Arrays.sort(indices);
List<Integer> curr_result = new ArrayList<Integer>();
curr_result.add(num[indices[0]]);
curr_result.add(num[indices[1]]);
curr_result.add(num[indices[2]]);
curr_result.add(num[indices[3]]);
result_set.add(curr_result);
}
}
}
}
}
result.addAll(result_set);
return result;
}
public static void main (String[] args) {
int[] num = {-3, -1, 0, 2, 4, 5};
int target = 0;
Solution sol = new Solution();
System.out.println(sol.fourSum(num, target));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.