12ms C++ Solution with Unordered Map


  • 0
    R
    class Solution {
    public:
    	vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
    		unordered_map<int, int> map;
    		vector<int> result;
    		for (int i = 0; i < nums1.size(); i++)
    			map[nums1[i]] = 0;
    		for (int i = 0; i < nums2.size(); i++)
    			if (map.find(nums2[i]) != map.end() && map[nums2[i]] == 0)
    			{
    				result.push_back(nums2[i]);
    				++map[nums2[i]];
    			}
    		return result;
    	}
    };

Log in to reply
 

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