349.Intersection of Two AND 350. Intersection of Two Arrays II Arrays leetcode binary search

1、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.
Subscribe to see which companies asked this question

方法一、采用hash表的方法进行实现,首先将所有出现在array1中的数字存放在一个map1中,key为数字,值为数字出现的次数,同理对array2也做同样的处理,存放在map2,最后遍历map1,查看map1中的key值是否出现在map2中,如果在则是两个的交
(1)一开始写得不好的代码,太长
时间复杂度为O(max(len1,len2)),空间复杂度为O(len1+len2)

vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        map<int,int> mpOne;
        map<int,int> mpTwo;

        int len1 = nums1.size();
        int len2 = nums2.size();
        if(len1 == 0 || len2 == 0)
        {
            return (vector<int> ());
        }

        vector<int> result;
        map<int,int>::iterator mp1;
        map<int,int>::iterator mp2;

        mpOne.insert(pair<int,int>(nums1[0],1));
        for(int i = 1; i < len1; i++)
        {
            mp1=mpOne.find(nums1[i]);
            if( mp1 != mpOne.end())
            {
                (*mp1).second++;
            }
            else
            {
                mpOne.insert(pair<int,int>(nums1[i],1));
            }
        }

        mpTwo.insert(pair<int,int>(nums2[0],1));
        for(int i = 1; i < len2; i++)
        {
            mp2=mpTwo.find(nums2[i]);
            if( mp2 != mpTwo.end())
            {
                (*mp2).second++;
            }
            else
            {
                mpTwo.insert(pair<int,int>(nums2[i],1));
            }
        }

        for(mp1=mpOne.begin(); mp1!=mpOne.end(); mp1++)
        {
            mp2 = mpTwo.find((*mp1).first);
            if(mp2 != mpTwo.end())
            {
                result.push_back((*mp1).first);
            }
        }

        return result;
    }

(2)改进后的版本一代码,依然采用map的思想,只是不用再去建立mapTwo
由于存在sort函数,故而时间复杂度为O(nlgn),空间复杂度为O(len1),由于map的存在

vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        //由于存在sort函数,故而时间复杂度为O(nlgn),空间复杂度为O(N),由于map的存在
        int len1 = nums1.size();
        int len2 = nums2.size();

        if(0 == len1 || 0 == len2)
            return (vector<int>());

        sort(nums1.begin(),nums1.end());
        sort(nums2.begin(),nums2.end());
        map<int,int> mapOne;
        vector<int> result;
        map<int,int>::iterator mp1;
        mapOne.insert(pair<int,int>(nums1[0],1));
        for(int i = 1; i < len1; i++)
        {
            mp1 = mapOne.find(nums1[i]);
            if(mp1 != mapOne.end() )
                (*mp1).second++;
            else
                mapOne.insert(pair<int,int>(nums1[i],1));
        }

        for(int i = 0; i < len2; i++)
        {
            mp1 = mapOne.find(nums2[i]);
            if(mp1 != mapOne.end())
            {
                //cout<<(*mp1).first<<" Two "<<(*mp1).second<<endl;
                if(result.empty() == 1)
                    result.push_back(nums2[i]); //注意如果vector为空的话,直接将nums2[i]加入vector,此时如果不加该句判断则会导致runtime 错误
                else 
                {
                    if(result.back() != nums2[i])
                        result.push_back(nums2[i]);
                }
            }
        }

        return result;
    }

方法二、采用数组的思想来解决
采用比较数组的思想来解决,时间复杂度为O(nlgn)

vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        //采用数组的思想来解决,时间复杂度为O(nlgn)
        int len1 = nums1.size();
        int len2 = nums2.size();

        if(0 == len1 || 0 == len2)
            return vector<int>();

        sort(nums1.begin(),nums1.end());
        sort(nums2.begin(),nums2.end());

        int i = 0;
        int j = 0;
        vector<int> result;
        for(; i < len1&&j < len2; i++,j++)
        {
            if(nums1[i] == nums2[j])
            {
                if(result.empty() == 1)
                {
                    result.push_back(nums1[i]);
                }
                else
                {
                    if(result.back() != nums1[i])
                        result.push_back(nums1[i]);
                }
            }
            else if(nums1[i] < nums2[j])
            {
                //i++;
                j--;//注意此处为j--,因为for循环每次会自动i++,j++,如果nums1[i]<nums2[j],nums1向前进一步,nums2仍然保持原地,而for循环会自动为j+1,故而此处为j--
            }
            else if(nums1[i] > nums2[j])
            {
                //j++;
                i--;
            }
        }

        return result;
    }

2、 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?
Subscribe to see which companies asked this question
以下算法的时间复杂度为O(NlgN ),如果用map则可以利用空间换时间

vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        int len1 = nums1.size();
        int len2 = nums2.size();

        if(0 == len1 || 0 == len2)
            return vector<int>();

        sort(nums1.begin(),nums1.end());
        sort(nums2.begin(),nums2.end());

        int i = 0; 
        int j = 0;
        vector<int> result;
        for(; i < len1 && j < len2; i++,j++)
        {
            if(nums1[i] == nums2[j])
                result.push_back(nums1[i]);
            else if(nums1[i] < nums2[j])
                j--;
            else
                i--;
        }

        return result;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值