zoukankan      html  css  js  c++  java
  • 【LeetCode & 剑指offer刷题】数组题10:Intersection of Two Arrays(系列)

    【LeetCode & 剑指offer 刷题笔记】目录(持续更新中...)

    349. 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.

     
    /*
    问题:求两个数组的交集(不能有重复的数)
    方法一:hash表法,用set避免重复
    O(n) ,O(n)
    */
    class Solution
    {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
        {
            if(nums1.empty() || nums2.empty()) return {};
           
            unordered_set<int> m(nums1.begin(), nums1.end()); //vector转set,避免重复
            vector<int> res;
            for (auto a : nums2)
            {
                if (m.count(a))
                {
                    res.push_back(a);
                    m.erase(a); //去除该数
                }          
            }
            return res;
        }
    };
    /*
    方法二:排序之后再找
    用时间来换空间
    O(nlogn),O(1)
    */
    class Solution
    {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2)
        {
            if(nums1.empty() || nums2.empty()) return {};
           
            vector<int> res;
            int i = 0, j = 0;
            sort(nums1.begin(), nums1.end());
            sort(nums2.begin(), nums2.end());
           
            while (i < nums1.size() && j < nums2.size()) //从起始位置开始分别扫描两个数组
            {
                if (nums1[i] < nums2[j]) i++; //递增第一个数组的指针
                else if (nums1[i] > nums2[j]) j++; //递增第二个数组的指针
                else
                {
                    if (res.empty() || res.back() != nums1[i]) //如果为空或者不重复时才push交集元素
                    {
                        res.push_back(nums1[i]);
                    }
                    i++; j++; //同时递增两个数组指针
                }
            }
            return res;
        }
    };
     
     
     
    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?
    /*问题:计算两个数组的交集(交集中元素与顺序无关,不是指公共子数组)
    可以返回重复的数
    思考题3
    If only nums2 cannot fit in memory, put all elements of nums1 into a HashMap, read chunks of array that fit into the memory, and record the intersections.
    If both nums1 and nums2 are so huge that neither fit into the memory, sort them individually (external sort), then read 2 elements from each array at a time in memory, record intersections.
    */
    /*
    * 方法一:hash
    * 过程:构建hash表计数器,依nums2在计数器中出现的次数,如果出现了,就push元素,计数器对应值减一
    * 分析:O(m+n),O(m)
    */
    #include <unordered_map>
    class Solution
    {
    public:
        vector<int> intersect(vector<int>& nums1, vector<int>& nums2)
        {
            unordered_map<int, int> counter;
            vector<int> res;
            for(int num:nums1) counter[num]++;  // 统计每个数的出现次数(首次调用 operator[] 以零初始化计数器
            for(int num:nums2)
            {
                if(counter.find(num) != counter.end() && counter[num]>=1)//如果在nums1中找得到对应元素而且计数值大于1时,说明是交集的一部分
                {
                    counter[num]--;
                    res.push_back(num);
                }
            }
            return res;
        }
    };
    /*
    * 方法二:排序之后再找
    以时间换空间
    * 分析:Omlogm +nlogn+m+n),O(1)
    */
    #include <algorithm>
    class Solution
    {
    public:
        vector<int> intersect(vector<int>& nums1, vector<int>& nums2)
        {
            if(nums1.empty() || nums2.empty()) return {};
     
            vector<int> res;
            int i=0,j=0;       
            sort(nums1.begin(), nums1.end());
            sort(nums2.begin(), nums2.end());
     
            while(i < nums1.size() && j < nums2.size()) //ij分别用来遍历两个数组
            {
                if (nums1[i] < nums2[j]) i++; //递增第一个数组的指针
                else if (nums1[i] > nums2[j]) j++; //递增第二个数组的指针
                else
                {
                    res.push_back(nums1[i]);
                    i++; j++; //同时递增两个数组指针
                }
            }
            return res;
          
        }
    };
     
     
  • 相关阅读:
    宠物商店项目需求
    使用Ajax新闻系统管理需求分析
    java 面向对象面试题,问答题,构造方法,抽象类,继承,多态,接口,异常总结;
    如何在linux服务器部署Rstudio server,配置ODBC远程访问win 服务器上的SQL server
    R语言网络爬虫学习 基于rvest包
    用蒙特卡洛方法计算派-python和R语言
    R 语言学习日志 1
    kmeans聚类中的坑 基于R shiny 可交互的展示
    分类算法简介 基于R
    R 多线程和多节点并行计算
  • 原文地址:https://www.cnblogs.com/wikiwen/p/10224338.html
Copyright © 2011-2022 走看看