zoukankan      html  css  js  c++  java
  • 349. Intersection of Two Arrays

    Given two arrays, write a function to compute their intersection.

    Example 1:

    Input: nums1 = [1,2,2,1], nums2 = [2,2]
    Output: [2]
    

    Example 2:

    Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
    Output: [9,4]

    Note:

    • Each element in the result must be unique.
    • The result can be in any order.

    AC code:

    class Solution {
    public:
        vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
            int len1 = nums1.size();
            int len2 = nums2.size();
            vector<int> ans;
            map<int, int> mp;
            if (len1 == 0 || len2 == 0) return ans;
            for (int i = 0; i < len1; ++i) {
                mp[nums1[i]] = 1;
            }
            for (int i = 0; i < len2; ++i) {
                if (mp[nums2[i]] == 1) {
                    ans.push_back(nums2[i]);
                    mp[nums2[i]]--;
                }
            }
            return ans;
        }
    };
    

      

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    Docker
    Web
    爬虫
    Python
    软件脱壳
    网络抓包
    HTTPS单向认证,双向认证
    新版无完整背景图片滑块验证码
    Frida Hook
    闭包函数与装饰器
  • 原文地址:https://www.cnblogs.com/h-hkai/p/9880043.html
Copyright © 2011-2022 走看看