Skip to content

Commit 7783f3f

Browse files
all solutions of Intersection of Two Arrays
1 parent 172be00 commit 7783f3f

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

EASY/src/easy/IntersectionOfTwoArrays.java

+22-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,28 @@ public class IntersectionOfTwoArrays {
2222
//then I clicked its Tags, and find it's marked with so many tags: Binary Search, HashTable, Two Pointers, Sort, now I'll try to do it one by one
2323
//inspired by this post: https://discuss.leetcode.com/topic/45685/three-java-solutions
2424
public int[] intersection_two_pointers(int[] nums1, int[] nums2) {
25-
25+
Set<Integer> set = new HashSet();
26+
Arrays.sort(nums1);
27+
Arrays.sort(nums2);
28+
int i = 0, j = 0;
29+
for(; i < nums1.length && j < nums2.length;){
30+
if(nums1[i] < nums2[j]){
31+
i++;
32+
} else if(nums1[i] > nums2[j]){
33+
j++;
34+
} else {
35+
set.add(nums1[i]);
36+
i++;
37+
j++;
38+
}
39+
}
40+
int[] result = new int[set.size()];
41+
Iterator<Integer> it = set.iterator();
42+
int k = 0;
43+
while(it.hasNext()){
44+
result[k++] = it.next();
45+
}
46+
return result;
2647
}
2748

2849
public int[] intersection_binary_search(int[] nums1, int[] nums2) {

0 commit comments

Comments
 (0)