题解 | #两个数组的交集#

两个数组的交集

https://www.nowcoder.com/practice/56ea71d1f4e94de2aaec10e985874cce

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) 
    {
        //建立一个哈希数组
        bool hash[1010] = {0};

        //将答案存放到ret中
        vector<int> ret;
        //将 nums1的元素标记为true
        for(auto e : nums1)
        {
            hash[e] = true;
        }

        //遍历nums2
        for(auto e : nums2)
        {
            //若为真为公共数字
            if(hash[e])
            {
                //存到ret中
                ret.push_back(e);
                //避免重复,所以在hash中删除这个数字
                hash[e] = false;
            }
        }
        return ret;
    }
};

全部评论

相关推荐

03-25 16:22
南华大学 Java
不敢追175女神:你是打了上千个招呼吧?😂
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务