C++ 6ms O(m+n) unordered_map solution


  • 0
    C
    class Solution {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            unordered_map<int, bool> uniq;
            vector<int> result;
            
            
            for(int i = 0; i < nums1.size(); i++){
                if(uniq.count(nums1[i]) == 0) uniq[nums1[i]] = 1;
            }
            
            for(int j = 0; j < nums2.size(); j++){
                if(uniq.count(nums2[j]) == 1 && uniq[nums2[j]] == 1){
                    result.push_back(nums2[j]);
                    uniq[nums2[j]] = 0;
                }
            }
            
            return result;
        }
    };
    

Log in to reply
 

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