c++ 12ms using unordered_map


  • 0
    X
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            unordered_map<int, int> m1;
            vector<int> ans;
            for(int i = 0;i < nums1.size(); ++i){
                m1[nums1[i]] = 0;
            }
            for(int j = 0; j < nums2.size(); ++j){
                if(m1.find(nums2[j]) != m1.end() && m1[nums2[j]] == 0){
                    ans.push_back(nums2[j]);
                    m1[nums2[j]] = 1;
                }
            }
            return ans;
        }
    

Log in to reply
 

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