forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersect.java
More file actions
77 lines (72 loc) · 1.91 KB
/
Intersect.java
File metadata and controls
77 lines (72 loc) · 1.91 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package normal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
/**
* @program JavaBooks
* @description: 350.两个数组的交集2
* @author: mf
* @create: 2019/11/06 16:14
*/
/*
题目:https://leetcode-cn.com/problems/intersection-of-two-arrays-ii/
类型:哈希和集合
难度:easy
*/
public class Intersect {
public static void main(String[] args) {
int[] nums1 = {1,2,2,1};
int[] nums2 = {2,2};
int[] ans = intersect2(nums1, nums2);
System.out.println(Arrays.toString(ans));
}
/**
* 集合
* @param nums1
* @param nums2
* @return
*/
private static int[] intersect(int[] nums1, int[] nums2) {
ArrayList<Integer> list1 = new ArrayList<>();
for (int num : nums1) {
list1.add(num);
}
ArrayList<Integer> list2 = new ArrayList<>();
for (int num : nums2) {
if (list1.contains(num)) {
list2.add(num);
list1.remove(num);
}
}
int[] ans = new int[list2.size()];
for (int i = 0; i < list2.size(); i++) {
ans[i] = list2.get(i);
}
return ans;
}
/**
* 哈希
* @param nums1
* @param nums2
* @return
*/
private static int[] intersect2(int[] nums1, int[] nums2) {
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums1) {
map.put(num, map.getOrDefault(num, 0) + 1);
}
ArrayList<Integer> list = new ArrayList<>();
for (int num : nums2) {
Integer count = map.get(num);
if (count != null && count != 0) {
list.add(num);
map.put(num, --count);
}
}
int[] ans = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
ans[i] = list.get(i);
}
return ans;
}
}