Intersection of Two Arrays I & II

题目链接:https://leetcode.com/problems/intersection-of-two-arrays/

题目大意:要求两个数组的交集(注意集合是不能含有重复的元素的)

方法1)

先对两个数组进行排序,设置两个指针pA和pB,分别指向这两个数组,比较nums1[pA]和nums[pB]

a. 如果想等,则为交集中的元素,++pA, ++pB

b. 如果nums[pA] < nums[pB],则++pA

c. 否则,++pB

注意数组中有重复的元素(实现代码中的小trick)

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
    {
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());

        int n1 = nums1.size();
        int n2 = nums2.size();
        int i = 0;
        int j = 0;
        vector<int> ans;
        while(i < n1 && j < n2)
        {
            while(i != 0 && nums1[i] == nums1[i-1] && i < n1)
            {
                ++i;
            }

            while(j != 0 && nums2[j] == nums2[j-1] && j < n2)
            {
                ++j;
            }

            if(i < n1 && j < n2)
            {
                if(nums1[i] == nums2[j])
                {
                    ans.push_back(nums1[i]);
                    ++i;
                    ++j;
                }
                else if(nums1[i] < nums2[j])
                {
                    ++i;
                }
                else
                {
                    ++j;
                }
            }
        }

        return ans;
    }
};

上述算法的时间复杂度为:

每一步的时间复杂度为O(n1log1), O(n2log2), (n1,n2)的最小值,最终的时间复杂度为O(n1log1)和O(n2log2)的最小值

方法2)

使用hash的方法来完成,因为题目当中没有要求交集中的数组是有序的

思路就不说了,直接看下面的代码吧

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
    {
        int n1 = nums1.size();
        int n2 = nums2.size();
        unordered_set<int> sets;
        for(int i=0; i<n1; ++i)
        {
            if(sets.find(nums1[i]) == sets.end())
            {
                sets.insert(nums1[i]);
            }
        }

        unordered_set<int> ans_sets;
        for(int i=0; i<n2; ++i)
        {
            if(sets.find(nums2[i]) != sets.end())
            {
                ans_sets.insert(nums2[i]);
            }
        }

        vector<int> ans(ans_sets.begin(), ans_sets.end());
        return ans;
    }
};

题目II在I的基础上加上了条件:就是可以出现重复的元素,其实是更加简单了,只要去掉去重的语句即可

题目最后给出了3个问题:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1‘s size is small compared to nums2‘s size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

1)如果数组已经有序,上述算法中算法1)的时间复杂度就是min(n1, n2)

2) 如果n1 < n2, 上述算法1更好,因为算法2)要遍历所有的元素

3)可以把num1中的数组放到map中,map记录当前元素以及出现的次数,然后读文件,遍历num2中的元素,如果出现在map中就输出一次,同时次数减1

时间: 2024-12-21 12:02:25

Intersection of Two Arrays I & II的相关文章

[LeetCode] 349 Intersection of Two Arrays &amp; 350 Intersection of Two Arrays II

这两道题都是求两个数组之间的重复元素,因此把它们放在一起. 原题地址: 349 Intersection of Two Arrays :https://leetcode.com/problems/intersection-of-two-arrays/description/ 350 Intersection of Two Arrays II:https://leetcode.com/problems/intersection-of-two-arrays-ii/description/ 题目&解法

[LeetCode][Python]Intersection of Two Arrays II

Intersection of Two Arrays II Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays

[LintCode] Intersection of Two Arrays II 两个数组相交之二

Given two arrays, write a function to compute their intersection.Notice Each element in the result should appear as many times as it shows in both arrays.    The result can be in any order. Example Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [

【leetcode】350. Intersection of Two Arrays II

problem 350. Intersection of Two Arrays II 参考 1. Leetcode_350. Intersection of Two Arrays II; 完 原文地址:https://www.cnblogs.com/happyamyhope/p/10430296.html

[LeetCode] Intersection of Two Arrays II 两个数组相交之二

Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. Note: Each element in the result should appear as many times as it shows in both arrays. The result can be in any ord

leetcode349 350 Intersection of Two Arrays &amp; II

1 """ 2 Intersection of Two Arrays 3 Given two arrays, write a function to compute their intersection. 4 Example 1: 5 Input: nums1 = [1,2,2,1], nums2 = [2,2] 6 Output: [2] 7 Example 2: 8 Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 9 Output:

[LintCode] Intersection of Two Arrays 两个数组相交

Given two arrays, write a function to compute their intersection.Notice Each element in the result must be unique.    The result can be in any order. Have you met this question in a real interview?Example Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], r

[LeetCode][Python]Intersection of Two Arrays

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. https://leet

Leetcode刷题记录[python]——349 Intersection of Two Arrays

一.前言 做了两题才慢慢摸清了leetcode的操作. 二.题349 Intersection of Two Arrays Given two arrays, write a function to compute their intersection. class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int]