-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFindTheDifferenceOfTwoArrays.java
More file actions
42 lines (35 loc) · 1.06 KB
/
FindTheDifferenceOfTwoArrays.java
File metadata and controls
42 lines (35 loc) · 1.06 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
package array;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Description: https://leetcode.com/problems/find-the-difference-of-two-arrays
* Difficulty: Easy
* Time complexity: O(m + n)
* Space complexity: O(m + n)
*/
public class FindTheDifferenceOfTwoArrays {
public static void main(String[] args) {
int n = 5;
int times = n / 4 + (n % 4 == 0 ? 0 : 1);
System.out.println(times);
}
public List<List<Integer>> findDifference(int[] nums1, int[] nums2) {
Set<Integer> set2 = new HashSet<>();
for (int num : nums2) {
set2.add(num);
}
Set<Integer> set1 = new HashSet<>();
List<Integer> result1 = new ArrayList<>();
for (int num : nums1) {
set1.add(num);
if (set2.add(num)) result1.add(num);
}
List<Integer> result2 = new ArrayList<>();
for (int num : nums2) {
if (set1.add(num)) result2.add(num);
}
return List.of(result1, result2);
}
}