-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntersectionOfTwoArrayslI350.java
More file actions
37 lines (32 loc) · 1.01 KB
/
IntersectionOfTwoArrayslI350.java
File metadata and controls
37 lines (32 loc) · 1.01 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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class IntersectionOfTwoArrayslI350 {
/*
* Given two integer arrays nums1 and nums2, return an array of their intersection.
* Each element in the result must appear as many times as it shows in both arrays and you may return the result in any order.
*/
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
List<Integer> ans = new ArrayList<>() ;
int l2 = nums2.length ;
Arrays.sort(nums2) ;
Arrays.sort(nums1) ;
int j = 0 ;
for (int i : nums1){
while(j < l2 && nums2[j] <= i){
if (i == nums2[j]){
ans.add(i);
j++;
break;
}
j++;
}
}
int [] arr = new int[ans.size()] ;
j = 0;
for (int k : ans) arr[j++] = k ;
return arr ;
}
}
}