simple C++ solution 6ms


  • 0
    S
    class Solution {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            vector<int> tmp(1000);
            vector<int> ans;
            
            for(int i=0 ; i<nums1.size() ; i++)//record array1 val.
                tmp[nums1[i]]++;
            
            for(int j=0 ; j<nums2.size() ; j++)//check intersection
                if(tmp[nums2[j]]!=0)
                {
                    ans.push_back(nums2[j]);
                    tmp[nums2[j]]=0;
                }
            return ans;
        }
    };
    

  • 0
    D

    Do you consider about the size of the nums1 and nums2 ?


Log in to reply
 

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