C++ hashtable based O(N) solution


  • 0
    F
    class Solution {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            unordered_map<int, int> hashmap;
            for (auto i : nums1) {
                hashmap[i] = 1;
            }
            for (auto i : nums2) {
                if (hashmap.find(i) != hashmap.end()) {
                    hashmap[i] = 2;
                }
            }
            vector<int> r;
            for (auto i : hashmap) {
                if (i.second == 2) {
                    r.push_back(i.first);
                }
            }
            return r;
        }
    };
    

Log in to reply
 

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