-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersect.java
37 lines (33 loc) · 959 Bytes
/
intersect.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
package leetcode;
import java.util.Arrays;
import java.util.HashMap;
public class intersect {
public int[] Intersect(int[] nums1, int[] nums2) {
if(nums1.length>nums2.length)
return Intersect(nums2,nums1);
HashMap<Integer,Integer> map = new HashMap<>();
for(int i:nums1) {
map.put(i, map.getOrDefault(i, 0)+1);
}
int k=0;
for(int i:nums2) {
int cnt = map.getOrDefault(i, 0);
if(cnt>0) {
nums1[k++] = i;
map.put(i,cnt-1);
}
}
return Arrays.copyOfRange(nums1, 0, k);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums1 = new int[]{1,2,1,2,1};
int[] nums2 = new int[] {2,2};
int[] nums3;
intersect it = new intersect();
nums1 = it.Intersect(nums1, nums2);
for(int i=0;i<nums1.length;i++) {
System.out.print(" "+nums1[i]);
}
}
}