16ms c++ solution


  • 0
    W
    class Solution {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            vector<int> res;
            unordered_map<int,int> num2cnt;
            for (auto i=0; i<nums1.size(); ++i) {
            	num2cnt[nums1[i]] = 1;
            } 
            for (auto i=0; i<nums2.size(); ++i) {
            	if (num2cnt[nums2[i]] == 1) {
            		num2cnt[nums2[i]] = 0;
            		res.push_back(nums2[i]);
            	}
            }
            return res;
        }
    };

  • 1
    A
    class Solution {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            map<int,int> m;
            vector<int> v1;
            int i;
            for(i=0;i<nums1.size();i++)
                m[nums1[i]]=1;
            for(i=0;i<nums2.size();i++)
                if(m[nums2[i]])
                {
                    v1.push_back(nums2[i]);
                    m[nums2[i]]=0;
                }
            return v1;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.