Leetcode 349. Intersection of Two Arrays (Easy) (cpp)

Leetcode 349. Intersection of Two Arrays (Easy) (cpp)

Tag: Hash Table, Two Pointers, Binary Search, Sort

Difficulty: Easy


class Solution {
public:
    vector intersection(vector& nums1, vector& nums2) {
        vector result;
        if (nums1.empty() || nums2.empty()) return result;
        unordered_map mapping;
        for (auto i : nums1) {
            mapping[i] = true;
        }
        for (auto i : nums2) {
            if (mapping[i]) {
                result.push_back(i);
                mapping.erase(i);
            }
        }
        return result;
    }
};

Leetcode 349. Intersection of Two Arrays (Easy) (cpp)_第1张图片

你可能感兴趣的:(Leetcode,C++,C++,Leetcode,Binary,Search,Leetcode,Hash,Table,Leetcode,Sort,Leetcode,Two,Pointers)