-
Notifications
You must be signed in to change notification settings - Fork 0
/
2215. 找出两数组的不同.java
51 lines (43 loc) · 1.32 KB
/
2215. 找出两数组的不同.java
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
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Main {
public static List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
// 使用HashSet去除重复元素
Set<Integer> set1 = new HashSet<>();
for (int num : nums1) {
set1.add(num);
}
Set<Integer> set2 = new HashSet<>();
for (int num : nums2) {
set2.add(num);
}
// 找到nums1中的不同元素
List<Integer> diff1 = new ArrayList<>();
for (int num : set1) {
if (!set2.contains(num)) {
diff1.add(num);
}
}
// 找到nums2中的不同元素
List<Integer> diff2 = new ArrayList<>();
for (int num : set2) {
if (!set1.contains(num)) {
diff2.add(num);
}
}
List<List<Integer>> answer = new ArrayList<>();
answer.add(diff1);
answer.add(diff2);
return answer;
}
public static void main(String[] args) {
int[] nums1 = {1, 2, 3};
int[] nums2 = {2, 4, 6};
System.out.println(findDifference(nums1, nums2));
int[] nums3 = {1, 2, 3, 3};
int[] nums4 = {1, 1, 2, 2};
System.out.println(findDifference(nums3, nums4));
}
}