Leetcode: Intersection of Two Arrays

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:
Each element in the result must be unique.
The result can be in any order.

Use two hash sets

Time complexity: O(n)

注意Set<Integer> set = new HashSet<>(); 第二个泛型可以不写出

 1 public class Solution {
 2     public int[] intersection(int[] nums1, int[] nums2) {
 3         Set<Integer> set = new HashSet<>();
 4         Set<Integer> intersect = new HashSet<>();
 5         for (int i = 0; i < nums1.length; i++) {
 6             set.add(nums1[i]);
 7         }
 8         for (int i = 0; i < nums2.length; i++) {
 9             if (set.contains(nums2[i])) {
10                 intersect.add(nums2[i]);
11             }
12         }
13         int[] result = new int[intersect.size()];
14         int i = 0;
15         for (Integer num : intersect) {
16             result[i++] = num;
17         }
18         return result;
19     }
20 }

Iterator iter = set2.iterator();
for (int i=0; i<res.length; i++) {
res[i] = (int)iter.next();
}

用iterator也可以,但要注意.next()之后要转换为int

Sort both arrays, use two pointers

Time complexity: O(nlogn)

 1 public class Solution {
 2     public int[] intersection(int[] nums1, int[] nums2) {
 3         Set<Integer> set = new HashSet<>();
 4         Arrays.sort(nums1);
 5         Arrays.sort(nums2);
 6         int i = 0;
 7         int j = 0;
 8         while (i < nums1.length && j < nums2.length) {
 9             if (nums1[i] < nums2[j]) {
10                 i++;
11             } else if (nums1[i] > nums2[j]) {
12                 j++;
13             } else {
14                 set.add(nums1[i]);
15                 i++;
16                 j++;
17             }
18         }
19         int[] result = new int[set.size()];
20         int k = 0;
21         for (Integer num : set) {
22             result[k++] = num;
23         }
24         return result;
25     }
26 }

Binary search

Time complexity: O(nlogn)

 1 public class Solution {
 2     public int[] intersection(int[] nums1, int[] nums2) {
 3         Set<Integer> set = new HashSet<>();
 4         Arrays.sort(nums2);
 5         for (Integer num : nums1) {
 6             if (binarySearch(nums2, num)) {
 7                 set.add(num);
 8             }
 9         }
10         int i = 0;
11         int[] result = new int[set.size()];
12         for (Integer num : set) {
13             result[i++] = num;
14         }
15         return result;
16     }
17     
18     public boolean binarySearch(int[] nums, int target) {
19         int low = 0;
20         int high = nums.length - 1;
21         while (low <= high) {
22             int mid = low + (high - low) / 2;
23             if (nums[mid] == target) {
24                 return true;
25             }
26             if (nums[mid] > target) {
27                 high = mid - 1;
28             } else {
29                 low = mid + 1;
30             }
31         }
32         return false;
33     }
34 }
原文地址:https://www.cnblogs.com/EdwardLiu/p/6096747.html