Skip to content

Commit 318e158

Browse files
refactor 349
1 parent 4f4f78c commit 318e158

File tree

2 files changed

+88
-29
lines changed

2 files changed

+88
-29
lines changed

src/main/java/com/fishercoder/solutions/_349.java

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,48 @@
22

33
import java.util.Arrays;
44
import java.util.HashSet;
5-
import java.util.Iterator;
65
import java.util.Set;
6+
import java.util.stream.Collectors;
77

88
/**
99
* 349. Intersection of Two Arrays
1010
*
1111
* Given two arrays, write a function to compute their intersection.
1212
*
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]
1416
*
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.
1624
*/
1725
public class _349 {
1826

1927
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)
2031
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()];
2436
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;
4239
}
43-
return result;
40+
return intersection;
4441
}
4542
}
4643

4744
public static class Solution2 {
45+
//Time: O(nlgn)
4846
public int[] intersection(int[] nums1, int[] nums2) {
49-
//this approach is O(nlgn)
50-
Arrays.sort(nums1);
5147
Arrays.sort(nums2);
5248
Set<Integer> intersect = new HashSet();
5349
for (int i : nums1) {
@@ -56,9 +52,9 @@ public int[] intersection(int[] nums1, int[] nums2) {
5652
}
5753
}
5854
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;
6258
}
6359
return result;
6460
}
@@ -79,4 +75,32 @@ private boolean binarySearch(int i, int[] nums) {
7975
return false;
8076
}
8177
}
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+
}
82106
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._349;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _349Test {
10+
private static _349.Solution1 solution1;
11+
private static _349.Solution2 solution2;
12+
private static _349.Solution3 solution3;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _349.Solution1();
17+
solution2 = new _349.Solution2();
18+
solution3 = new _349.Solution3();
19+
}
20+
21+
@Test
22+
public void test1() {
23+
assertArrayEquals(new int[]{2}, solution1.intersection(new int[]{1, 2, 2, 1}, new int[]{2, 2}));
24+
}
25+
26+
@Test
27+
public void test2() {
28+
assertArrayEquals(new int[]{2}, solution2.intersection(new int[]{1, 2, 2, 1}, new int[]{2, 2}));
29+
}
30+
31+
@Test
32+
public void test3() {
33+
assertArrayEquals(new int[]{2}, solution3.intersection(new int[]{1, 2, 2, 1}, new int[]{2, 2}));
34+
}
35+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy