2
2
3
3
import java .util .Arrays ;
4
4
import java .util .HashSet ;
5
- import java .util .Iterator ;
6
5
import java .util .Set ;
6
+ import java .util .stream .Collectors ;
7
7
8
8
/**
9
9
* 349. Intersection of Two Arrays
10
10
*
11
11
* Given two arrays, write a function to compute their intersection.
12
12
*
13
- * Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].
13
+ * Example 1:
14
+ * Input: nums1 = [1,2,2,1], nums2 = [2,2]
15
+ * Output: [2]
14
16
*
15
- * Note: Each element in the result must be unique. The result can be in any order.
17
+ * Example 2:
18
+ * Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
19
+ * Output: [9,4]
20
+ *
21
+ * Note:
22
+ * Each element in the result must be unique.
23
+ * The result can be in any order.
16
24
*/
17
25
public class _349 {
18
26
19
27
public static class Solution1 {
28
+ //credit: https://leetcode.com/articles/intersection-of-two-arrays/
29
+ //Time: O(m+n) on average, O(m*n) in worse case
30
+ //Space: O(m+n)
20
31
public int [] intersection (int [] nums1 , int [] nums2 ) {
21
- Set <Integer > set = new HashSet ();
22
- Arrays .sort (nums1 );
23
- Arrays .sort (nums2 );
32
+ Set <Integer > set1 = Arrays .stream (nums1 ).boxed ().collect (Collectors .toSet ());
33
+ Set <Integer > set2 = Arrays .stream (nums2 ).boxed ().collect (Collectors .toSet ());
34
+ set1 .retainAll (set2 );
35
+ int [] intersection = new int [set1 .size ()];
24
36
int i = 0 ;
25
- int j = 0 ;
26
- for (; i < nums1 .length && j < nums2 .length ; ) {
27
- if (nums1 [i ] < nums2 [j ]) {
28
- i ++;
29
- } else if (nums1 [i ] > nums2 [j ]) {
30
- j ++;
31
- } else {
32
- set .add (nums1 [i ]);
33
- i ++;
34
- j ++;
35
- }
36
- }
37
- int [] result = new int [set .size ()];
38
- Iterator <Integer > it = set .iterator ();
39
- int k = 0 ;
40
- while (it .hasNext ()) {
41
- result [k ++] = it .next ();
37
+ for (int num : set1 ) {
38
+ intersection [i ++] = num ;
42
39
}
43
- return result ;
40
+ return intersection ;
44
41
}
45
42
}
46
43
47
44
public static class Solution2 {
45
+ //Time: O(nlgn)
48
46
public int [] intersection (int [] nums1 , int [] nums2 ) {
49
- //this approach is O(nlgn)
50
- Arrays .sort (nums1 );
51
47
Arrays .sort (nums2 );
52
48
Set <Integer > intersect = new HashSet ();
53
49
for (int i : nums1 ) {
@@ -56,9 +52,9 @@ public int[] intersection(int[] nums1, int[] nums2) {
56
52
}
57
53
}
58
54
int [] result = new int [intersect .size ()];
59
- Iterator < Integer > it = intersect . iterator () ;
60
- for (int i = 0 ; i < intersect . size (); i ++ ) {
61
- result [i ] = it . next () ;
55
+ int i = 0 ;
56
+ for (int num : intersect ) {
57
+ result [i ++ ] = num ;
62
58
}
63
59
return result ;
64
60
}
@@ -79,4 +75,32 @@ private boolean binarySearch(int i, int[] nums) {
79
75
return false ;
80
76
}
81
77
}
78
+
79
+ public static class Solution3 {
80
+ //use two pointers
81
+ //credit: https://leetcode.com/problems/intersection-of-two-arrays/discuss/81969/Three-Java-Solutions
82
+ public int [] intersection (int [] nums1 , int [] nums2 ) {
83
+ Arrays .sort (nums1 );
84
+ Arrays .sort (nums2 );
85
+ int i = 0 ;
86
+ int j = 0 ;
87
+ Set <Integer > set = new HashSet <>();
88
+ while (i < nums1 .length && j < nums2 .length ) {
89
+ if (nums1 [i ] == nums2 [j ]) {
90
+ set .add (nums1 [i ++]);
91
+ j ++;
92
+ } else if (nums1 [i ] < nums2 [j ]) {
93
+ i ++;
94
+ } else {
95
+ j ++;
96
+ }
97
+ }
98
+ int [] intersection = new int [set .size ()];
99
+ int k = 0 ;
100
+ for (int num : set ) {
101
+ intersection [k ++] = num ;
102
+ }
103
+ return intersection ;
104
+ }
105
+ }
82
106
}
0 commit comments