forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT10.java
More file actions
38 lines (35 loc) · 915 Bytes
/
T10.java
File metadata and controls
38 lines (35 loc) · 915 Bytes
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
/**
* @program JavaBooks
* @description: 两个数组的交集 II
* @author: mf
* @create: 2020/04/09 17:55
*/
package subject.dpointer;
import java.util.ArrayList;
/**
* 输入: nums1 = [1,2,2,1], nums2 = [2,2]
* 输出: [2,2]
* 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
* 输出: [4,9]
*/
public class T10 {
// 这道题跟双指针没啥关系
public int[] intersect(int[] nums1, int[] nums2) {
ArrayList<Integer> list1 = new ArrayList<>();
for (int i : nums1) {
list1.add(i);
}
ArrayList<Integer> list2 = new ArrayList<>();
for (int i : nums2) {
if (list1.contains(i)) {
list2.add(i);
list1.remove(i);
}
}
int[] ans = new int[list2.size()];
for (int i = 0; i < list2.size(); i++) {
ans[i] = list2.get(i);
}
return ans;
}
}