Skip to content

Commit

Permalink
349. Intersection of Two Arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
liusishan committed Feb 20, 2019
1 parent 9c3fef4 commit 313d87e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @Auther: lss
* @Date: 2019/2/20 14:35
* @Description:
*/
public class Solution {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import java.util.ArrayList;
import java.util.TreeSet;

/**
* @Auther: lss
* @Date: 2019/2/20 14:35
* @Description:
*/
public class Solution {
public int[] intersection(int[] nums1, int[] nums2) {

TreeSet<Integer> set = new TreeSet<>();
for (int num : nums1)
set.add(num);

ArrayList<Integer> list = new ArrayList<>();
for (int num : nums2) {
if (set.contains(num)) {
list.add(num);
set.remove(num);
}
}
int[] res = new int[list.size()];
for (int i = 0; i < list.size(); i++)
res[i] = list.get(i);
return res;
}
}

0 comments on commit 313d87e

Please sign in to comment.