9-求交集问题

1. 两个数组的交集

在这里插入图片描述

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> s(nums1.begin(),nums1.end());
        unordered_set<int> result;
        for(auto n : nums2)
        {
        	//发现nums2的元素在result中出现过
            if(s.find(n) != s.end())
            {
                result.insert(n);
            }
        }
        return vector<int>(result.begin(),result.end());
    }
};

时间复杂度O(m+n),空间复杂度O(m+n)
相比于数组,使用set 不仅占用空间比数组大,而且速度要比数组慢,set把数值映射到key上都要做hash计算的。所以可以优化成使用数组

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int> result_set; // set是为了给结果集去重
        int hash[1005] = {0}; // 默认数值为0
        for (int num : nums1) { // nums1中出现的字母在hash数组中做记录
            hash[num] = 1;
        }
        for (int num : nums2) { // nums2中出现话,result记录
            if (hash[num] == 1) {
                result_set.insert(num);
            }
        }
        return vector<int>(result_set.begin(), result_set.end());
    }
};

2. 两个数组的交集II

在这里插入图片描述

class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        vector<int> result;
        int a[1001] = {0};
        int b[1001] = {0};
        for(auto n : nums1){
            a[n]++;
        }
        for(auto n : nums2){
            b[n]++;
        }
        int maxv = max(*max_element(nums1.begin(), nums1.end()), *max_element(nums2.begin(), nums2.end()));
        for(int i = 0; i <= maxv; ++i){
            a[i] = min(a[i], b[i]);
            while(a[i] != 0){
                result.push_back(i);
                a[i]--;
            }
        }
        return result;
    }
};

如果是有序的,如何优化?

class Solution {
public:
    vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
        int l1 = nums1.size(), l2 = nums2.size();
        vector<int> result;
        int i = 0, j = 0;
        while (i < l1 && j < l2) {
            if (nums1[i] < nums2[j]) {
                i++;
            } else if (nums1[i] > nums2[j]) {
                j++;
            } else {
                result.push_back(nums1[i]);
                i++;
                j++;
            }
        }
        return result;
    }
};

时间复杂度O(mlogm+nlogn),空间复杂度O(min(m,n))

如果 nums2 的元素存储在磁盘上,内存是有限的,并且你不能一次加载所有的元素到内存中,你该怎么办?

3. 查找共用字符

在这里插入图片描述

class Solution {
public:
    vector<string> commonChars(vector<string>& words) {
        vector<string> result;
        int a[26] = {0};// 用来统计所有字符串里字符出现的最小频率
        
        // 用第一个字符串给a初始化
        for(int i = 0; i < words[0].size(); ++i){
            a[words[0][i] - 'a']++;
        }
        for(int i = 1; i < words.size(); ++i){
            int b[26] = {0};
            for(int j = 0; j < words[i].size(); ++j){
                b[words[i][j] - 'a']++;
            }
            //求交集,//更新a,保证a里统计26个字符在所有字符串里出现的最小次数
            for(int k = 0; k < 26; ++k){
                a[k] = min(a[k], b[k]);
            }
        }

        for(int i = 0; i < 26; ++i){
            while(a[i] > 0){//多个重复的字符
                string s(1, i + 'a');
                result.push_back(s);
                a[i]--;
            }
        }
        return result;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值