#leetCode brush title documentary Day27

https://leetcode-cn.com/problems/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: = [4,9,5], nums2 = [9,4,9,8,4] nums1
Output: [9,4]
Description:

The output of each element must be unique.
We can not consider the order of output.

 

Chicken dishes to try:

As do a similar topic yesterday, directly modify the code a bit last optimal solution:

 1 class Solution {
 2 public:
 3     vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
 4         vector<int>rec;
 5         unordered_map<int,int>map;
 6         for(int i = 0; i < nums1.size(); i ++)
 7             map[nums1[i]] += 1;
 8         for(int i = 0;i < nums2.size(); i ++)
 9            if(map[nums2[i]] > 0) {
10                rec.push_back(nums2[i]);
11                map[nums2[i]] = 0;
12            }
13         return rec;
14     }
15 };

Day27 the blog specifically to see it!

 

 

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/intersection-of-two-arrays
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.

Guess you like

Origin www.cnblogs.com/xyy999/p/11938988.html