[LeetCode] 349. Intersection of Two Arrays

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [9,4]
Explanation: [4,9] is also accepted.

Constraints:

  • 1 <= nums1.length, nums2.length <= 1000
  • 0 <= nums1[i], nums2[i] <= 1000

两个数组的交集。

版本二350题。题目即是题意。

这道题有三种写法,1. 两个hashset;2. 双指针;3. 二分法。

首先是两个hashset的思路。用第一个hashset记录num1中的所有元素,然后遍历num2,看num2中每个元素是否在第一个hashset里面出现过,若出现过,则加入第二个hashset,最后用数组返回hashset所有的key。

时间O(n)

空间O(n)

Java实现

 1 class Solution {
 2     public int[] intersection(int[] nums1, int[] nums2) {
 3         // corner case
 4         if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) {
 5             return new int[] {};
 6         }
 7 
 8         // normal case
 9         HashSet<Integer> set1 = new HashSet<>();
10         HashSet<Integer> set2 = new HashSet<>();
11         for (Integer num : nums1) {
12             set1.add(num);
13         }
14         for (Integer num : nums2) {
15             if (set1.contains(num)) {
16                 set2.add(num);
17             }
18         }
19         int i = 0;
20         int[] res = new int[set2.size()];
21         for (Integer element : set2) {
22             res[i++] = element;
23         }
24         return res;
25     }
26 }

JavaScript实现

 1 /**
 2  * @param {number[]} nums1
 3  * @param {number[]} nums2
 4  * @return {number[]}
 5  */
 6 var intersection = function(nums1, nums2) {
 7     let set = new Set(nums1);
 8     let res = new Set();
 9     for (let i = 0; i < nums2.length; i++) {
10         if (set.has(nums2[i])) {
11             res.add(nums2[i]);
12         }
13     }
14     return Array.from(res);
15 };

再来是双指针的思路。也是需要用到一个hashset,因为要求输出unique的结果集。首先sort两个数组,接着用双指针分别遍历两个数组,看是否能找到一样的元素,加入hashset,最后返回的也是hashset的所有的key。

时间O(nlogn) - 因为有sort

空间O(n)

Java实现

 1 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 }

最后是二分法。先sort num2,对于num1中的每个元素,用二分法去查找是否存在于num2。

时间O(nlogn) - sort

空间O(n)

Java实现

 1 class Solution {
 2     public int[] intersection(int[] nums1, int[] nums2) {
 3         Set<Integer> set = new HashSet<>();
 4         Arrays.sort(nums2);
 5         for (int num : nums1) {
 6             if (helper(nums2, num)) {
 7                 set.add(num);
 8             }
 9         }
10         int i = 0;
11         int[] res = new int[set.size()];
12         for (int num : set) {
13             res[i++] = num;
14         }
15         return res;
16     }
17 
18     private boolean helper(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 }

LeetCode 题目总结

原文地址:https://www.cnblogs.com/cnoodle/p/12596378.html