[LeetCode]349. Intersection of Two Arrays(两数组的交集)

349. Intersection of Two Arrays

点击查看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].

Note:

  • Each element in the result must be unique.(结果中的每个元素必须是唯一的)
  • The result can be in any order.(结果可以是任何顺序)

思路1:

  • 设置一个数组res用来存放nums1内每个元素的个数,并将其赋值给元素大小对应的位置(例: nums1里面有5个3,则res[2] = 5)
  • 遍历nums2内元素,看元素大小对应位置是否为0,若不是,则将该元素大小在res中对应的位置添加至交集数组,并将其赋值为0
  • 最后返回交集数组
  • 简单的小数值数组倒是能够用,但是数组内最大元素大于1000,这个算法就不能用了
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        vector <int> res(1000, 0);//处理不了大量数据,不符合题意
        vector <int> intersectionArray;//交集数组
        for(char m : nums1)
            res[m]++;
        for(char n : nums2){
            if(res[n] != 0){
                 intersectionArray.push_back(n);//将两数组内相同的数存入数组intersectionArray中
                 res[n] = 0;//赋值为0是为了不存入相同元素
                 //例如: nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 第二个2不存入
            }
        }
        return intersectionArray;
    }

};

int main()
{
    Solution a;
    int A[4] = {2, 8, 11, 15};
    int B[4] = {2, 7, 21, 15};
    vector<int> nums1(A, A+4);
    vector<int> nums2(B, B+4);
    vector<int> intersectionArray = a.intersection(nums1, nums2);
    for(int i=0; i<intersectionArray.size(); i++)
        cout << intersectionArray[i] << " ";
    return 0;
}

思路2:

  • 由于思路1不能处理大量数据,只能用不定长的数组
  • 先将数组排序,同时遍历两个数组,碰见相同值就输入交集数组,并且派出两有序数组前后两个元素均相等的情况。
vector<int> intersection1(vector<int>& nums1, vector<int>& nums2) {
        vector <int> intersectionArray;//交集数组
        if(nums1.empty() || nums2.empty())//判断数组是否为空
            return intersectionArray;
        sort(nums1.begin(), nums1.end());//底层使用了快速排序,O(nlogn)
        sort(nums2.begin(), nums2.end());
        int m = nums1.size();
        int n = nums2.size();

        for(int i=0,j=0; i<m||j<n; ){//任何一个数组遍历完就结束循环
            if(nums1[i] == nums2[j]){
                if(i==0 || j==0){//若是数组首元素直接加入交集数组
                    intersectionArray.push_back(nums1[i]);
                    i++; j++;
                    continue;
                }
                //排除两个数组内均有两个或两个以上相同元素的情况。例如nums1=[1, 2, 2, 1],nums2=[2, 2]
                else if(nums1[i]!=nums1[i-1]){
                    intersectionArray.push_back(nums1[i]);
                    i++; j++;
                    continue;
                }
                //两个数组内均有两个或两个以上相同元素,i++ j++ 直接开始下一个循环
                else{
                    i++; j++;
                    continue;
                }

            }
            else if(nums1[i] > nums2[j])
                j++;
            else
                i++;
        }
        return intersectionArray;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值