# leetCode 350. Intersection of Two Arrays II 哈希

350. Intersection of Two Arrays II

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

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2, 2].

Note:

• Each element in the result should appear as many times as it shows in both arrays.

• The result can be in any order.

• What if the given array is already sorted? How would you optimize your algorithm?

• What if nums1's size is small compared to nums2's size? Which algorithm is better?

• What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class  Solution { public :      vector< int > intersect(vector< int >& nums1, vector< int >& nums2) {          vector< int > result;          multiset< int > set2;          for ( int  i = 0; i < nums2.size(); i++)              set2.insert(nums2[i]);          for ( int  i = 0; i < nums1.size(); i++)          {              if (set2.find(nums1[i]) != set2.end())              {                  result.push_back(nums1[i]);                  set2.erase(set2.find(nums1[i]));              }          }          return  result;      } };

|
30天前
《LeetCode》—— 哈希
《LeetCode》—— 哈希
24 0
|
30天前
leetcode-1044：最长重复子串(滚动哈希)
leetcode-1044：最长重复子串(滚动哈希)
34 0
|
9月前
|

LeetCode刷题---Two Sum（一）
LeetCode刷题---Two Sum（一）
|
5天前
|

LeetCode 题目 87：递归\动态规划\哈希实现 扰乱字符串
LeetCode 题目 87：递归\动态规划\哈希实现 扰乱字符串
5 0
|
5天前
|

LeetCode题目55:跳跃游戏【python5种算法贪心/回溯/动态规划/优化贪心/索引哈希映射 详解】
LeetCode题目55:跳跃游戏【python5种算法贪心/回溯/动态规划/优化贪心/索引哈希映射 详解】
15 4
|
30天前
[leetcode] 705. 设计哈希集合
[leetcode] 705. 设计哈希集合
21 2
|
30天前
leetcode-1001：网格照明（自定义哈希集合）
leetcode-1001：网格照明（自定义哈希集合）
22 0
|
7月前
Leetcode 4. Median of Two Sorted Arrays

18 0
|
9月前
|

|
12月前
|

LeetCode - #2 Add Two Numbers

55 0