【LeetCode 136】 关关的刷题日记33 Intersection of Two

时间:2022-05-07
本文章向大家介绍【LeetCode 136】 关关的刷题日记33 Intersection of Two,主要内容包括题目、思路、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

关关的刷题日记33 – Leetcode 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.

题目的意思是给定两个数组,求这两个数组的交集,而且要求结果中不能有重复的数。

思路

题目的意思是给定两个数组,求这两个数组的交集,而且要求结果中不能有重复的数。

class Solution {public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        set<int>t,t1;
        vector<int>output;
        for(int x:nums1)
        {
            t.insert(x);
        }
        for(int y:nums2)
        {
            if(t.find(y)!=t.end())
            {
                t.erase(y);
                output.push_back(y);
            }               
        }
        return output;
    }};

人生易老,唯有陪伴最长情,加油!

以上就是关关关于这道题的总结经验,希望大家能够理解,有什么问题可以在我们的专知公众号平台上交流或者加我们的QQ专知-人工智能交流群 426491390,也可以加入专知——Leetcode刷题交流群(请先加微信小助手weixinhao: Rancho_Fang,注明Leetcode刷题)。