Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. // https://leetcode.com/problems/intersection-of-two-arrays/
  2. /* Runtime: 0 ms, faster than 100.00% of Java online submissions for Intersection of Two Arrays.
  3.    Memory Usage: 37.6 MB, less than 58.11% of Java online submissions for Intersection of Two Arrays. */
  4. private static class UsingArray implements Solution {
  5.     public int[] intersection(int[] nums1, int[] nums2) {
  6.         if (nums1.length < nums2.length) {
  7.             int[] swap = nums1; nums1 = nums2; nums2 = swap;
  8.         }
  9.         boolean[] uniques = new boolean[1024];
  10.         for (int num : nums2) uniques[num] = true;
  11.         int index = -1;
  12.         int[] ans = new int[nums2.length];
  13.         for (int num : nums1) {
  14.             if (uniques[num]) {
  15.                 uniques[num] = false;
  16.                 ans[++index] = num;
  17.             }
  18.         }
  19.         return Arrays.copyOfRange(ans, 0, index + 1);
  20.     }
  21. }
  22.  
  23. /* Runtime: 1 ms, faster than 99.85% of Java online submissions for Intersection of Two Arrays.
  24.    Memory Usage: 37.6 MB, less than 58.11% of Java online submissions for Intersection of Two Arrays. */
  25. private static class UsingBitSet implements Solution {
  26.     public int[] intersection(int[] nums1, int[] nums2) {
  27.         if (nums1.length < nums2.length) {
  28.             int[] swap = nums1; nums1 = nums2; nums2 = swap;
  29.         }
  30.         BitSet uniques = new BitSet(256);
  31.         for (int num : nums2) uniques.set(num);
  32.  
  33.         int index = -1;
  34.         int[] ans = new int[nums2.length];
  35.         for (int num : nums1) {
  36.             if (uniques.get(num)) {
  37.                 uniques.clear(num);
  38.                 ans[++index] = num;
  39.             }
  40.         }
  41.         return Arrays.copyOfRange(ans, 0, index + 1);
  42.     }
  43. }
  44.  
  45. /* Runtime: 2 ms, faster than 97.92% of Java online submissions for Intersection of Two Arrays.
  46.    Memory Usage: 36.9 MB, less than 89.19% of Java online submissions for Intersection of Two Arrays. */
  47. private static class UsingHashSet implements Solution {
  48.     public int[] intersection(int[] nums1, int[] nums2) {
  49.         if (nums1.length < nums2.length) {
  50.             int[] swap = nums1; nums1 = nums2; nums2 = swap;
  51.         }
  52.         HashSet<Integer> uniques = new HashSet<>();
  53.         for (int num : nums2) uniques.add(num);
  54.  
  55.         int index = -1;
  56.         int[] ans = new int[uniques.size()];
  57.         for (int num : nums1) {
  58.             if (uniques.contains(num)) {
  59.                 uniques.remove(num);
  60.                 ans[++index] = num;
  61.             }
  62.         }
  63.         return Arrays.copyOfRange(ans, 0, index + 1);
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement