leetcode-350-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 order.

Follow up:

    • 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?

要完成的函数:

vector<int> intersect(vector<int>& nums1, vector<int>& nums2)

说明:

1、这道题给定两个vector,要求返回两个vector的交集,比如nums1=[1,2,2,1],nums2=[2,2],返回的交集是[2,2],其中有多少个相同的元素就返回多少个。返回的交集不讲究顺序。

2、这道题看完题意,熟悉leetcode的同学应该会马上想到先排序,排序之后的两个vector来比较,时间复杂度会下降很多。

如果不排序,那就是双重循环的做法,O(n^2),时间复杂度太高了。

先排序再比较的做法,代码如下:(附详解)

    vector<int> intersect(vector<int>& nums1, vector<int>& nums2)
    {
        sort(nums1.begin(),nums1.end());//给nums1排序,升序
        sort(nums2.begin(),nums2.end());//给nums2排序,升序
        int s1=nums1.size(),s2=nums2.size(),i=0,j=0;//i表示nums1元素的位置,j表示nums2元素的位置
        vector<int>res;//存储最后结果的vector
        while(i<s1&&j<s2)//两个vector一旦有一个遍历完了,那么就结束比较
        {
            if(nums1[i]<nums2[j])
            {
                while(nums1[i]<nums2[j]&&i<s1)//一直找,直到nums1[i]>=nums2[j]
                    i++;
                if(i==s1)//如果i已经到了nums1的外面,那么结束比较
                    break;
            }
            else if(nums1[i]>nums2[j])
            {
                while(nums1[i]>nums2[j]&&j<s2)//一直找,直到nums2[j]>=nums1[i]
                    j++;
                if(j==s2)//如果j已经到了nums2的外面,那么结束比较
                    break;
            }
            if(nums1[i]==nums2[j])//如果刚好相等,那么插入到res中,更新i和j的值
            {
                res.push_back(nums1[i]);
                i++;
                j++;
            }
        }
        return res;
    }

上述代码实测7ms,beats 98.05% of cpp submissions。

原文地址:https://www.cnblogs.com/king-3/p/9122890.html

时间: 2024-10-29 13:56:28

leetcode-350-Intersection of Two Arrays II(求两个数组的交集)的相关文章

Leetcode 350. Intersection of Two Arrays II JAVA语言

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 or

LeetCode 350. 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

[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】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

求两个数组的交集

问题: 给你两个排序的数组,求两个数组的交集. 比如: A = 1 3 4 5 7, B = 2 3 5 8 9, 那么交集就是 3 5. 思路: 1. 每一次从B数组中取一值,然后在A数组里逐个比较,如果有相等的,则保存.该算法复杂度为 O(MN). M, N 分别为数组 A B 的长度. 2. 因为A B 都排过序,所以,每一次从B数组取值后,可以利用二分查找看是否在数组A里有B所对应的值,这样复杂度变成了O(N lg M). 这里,如果N 比 M 大,可以从A中取值,然后在B中判断是否有A

[LeetCode] NO. 350 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]. [题目解析] 这是对于求两个数组交集的延伸,之前求数组交集重复元素不包含在交集内,用set来求.那么这个问题,就可以用hashmap来解决.如下. public int[] intersect(int[] nums1, int

leetcode修炼之路——350. 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

[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

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:

350 Intersection of Two Arrays II 两个数组的交集 II

给定两个数组,写一个方法来计算它们的交集.例如:给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2].注意:       输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致.       我们可以不考虑输出结果的顺序.跟进:    如果给定的数组已经排好序呢?你将如何优化你的算法?    如果 nums1 的大小比 nums2 小很多,哪种方法更优?    如果nums2的元素存储在磁盘上,内存是有限的,你不能一次加载所有的元素到内存