-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode2215.java
More file actions
46 lines (41 loc) · 1.38 KB
/
LeetCode2215.java
File metadata and controls
46 lines (41 loc) · 1.38 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
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import util.PrintUtil;
public class LeetCode2215 {
public static void main(String[] args) {
// 输入:nums1 = [1,2,3], nums2 = [2,4,6]
// 输出:[[1,3],[4,6]]
PrintUtil.printNestedList(new Solution2215().findDifference(new int[] { 1, 2, 3 }, new int[] { 2, 4, 6 }));
// 输入:nums1 = [1,2,3,3], nums2 = [1,1,2,2]
// 输出:[[3],[]]
PrintUtil
.printNestedList(new Solution2215().findDifference(new int[] { 1, 2, 3, 3 }, new int[] { 1, 1, 2, 2 }));
}
}
class Solution2215 {
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
HashSet<Integer> set1 = new HashSet<Integer>();
for (int i = 0; i < nums1.length; i++) {
set1.add(nums1[i]);
}
HashSet<Integer> set2 = new HashSet<Integer>();
for (int i = 0; i < nums2.length; i++) {
set2.add(nums2[i]);
}
List<List<Integer>> ans = new ArrayList<>();
ans.add(new ArrayList<>());
ans.add(new ArrayList<>());
for (int ele : set1) {
if (!set2.contains(ele)) {
ans.get(0).add(ele);
}
}
for (int ele : set2) {
if (!set1.contains(ele)) {
ans.get(1).add(ele);
}
}
return ans;
}
}