-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution3.java
44 lines (37 loc) · 1.03 KB
/
Solution3.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
package SetBasicsAndBSTSet;
import java.util.ArrayList;
import java.util.TreeMap;
/**
* @ Description: LeetCode 350题目
* @ Date: Created in 23:16 20/07/2018
* @ Author: Anthony_Duan
*/
public class Solution3 {
public int[] intersect(int[] nums1, int[] nums2) {
TreeMap<Integer, Integer> map = new TreeMap<>();
for (int num :
nums1) {
if (!map.containsKey(num)) {
map.put(num, 1);
} else {
map.put(num, map.get(num) + 1);
}
}
ArrayList<Integer> list = new ArrayList<>();
for (int num :
nums2) {
if (map.containsKey(num)) {
list.add(num);
map.put(num, map.get(num) - 1);
if (map.get(num) == 0) {
map.remove(num);
}
}
}
int[] res = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
res[i] = list.get(i);
}
return res;
}
}