{"id":3137,"date":"2018-07-14T09:50:42","date_gmt":"2018-07-14T16:50:42","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=3137"},"modified":"2018-07-14T09:50:52","modified_gmt":"2018-07-14T16:50:52","slug":"leetcode-350-intersection-of-two-arrays-ii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-350-intersection-of-two-arrays-ii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 350. Intersection of Two Arrays II"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<p>Given two arrays, write a function to compute their intersection.<\/p>\n<p><b>Example:<\/b><br \/>\nGiven\u00a0<i>nums1<\/i>\u00a0=\u00a0<code>[1, 2, 2, 1]<\/code>,\u00a0<i>nums2<\/i>\u00a0=\u00a0<code>[2, 2]<\/code>, return\u00a0<code>[2, 2]<\/code>.<\/p>\n<p><b>Note:<\/b><\/p>\n<ul>\n<li>Each element in the result should appear as many times as it shows in both arrays.<\/li>\n<li>The result can be in any order.<\/li>\n<\/ul>\n<p><b>Follow up:<\/b><\/p>\n<ul>\n<li>What if the given array is already sorted? How would you optimize your algorithm?<\/li>\n<li>What if\u00a0<i>nums1<\/i>&#8216;s size is small compared to\u00a0<i>nums2<\/i>&#8216;s size? Which algorithm is better?<\/li>\n<li>What if elements of\u00a0<i>nums2<\/i>\u00a0are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?<\/li>\n<\/ul>\n<h1><strong>Solution1: Hashtable<\/strong><\/h1>\n<p>Time complexity: O(m + n)<\/p>\n<p>Space complexity: O(m)<\/p>\n<pre class=\"lang:default decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;int&gt; intersect(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2) {\r\n    unordered_map&lt;int, int&gt; count;    \r\n    vector&lt;int&gt; ans;\r\n    for (int num : nums1)\r\n      ++count[num];\r\n    for (int num : nums2)\r\n      if (count.count(num) &amp;&amp;count[num] &gt; 0) {\r\n        --count[num];\r\n        ans.push_back(num);\r\n      }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given two arrays, write a function to compute their intersection. Example: Given\u00a0nums1\u00a0=\u00a0[1, 2, 2, 1],\u00a0nums2\u00a0=\u00a0[2, 2], return\u00a0[2, 2]. Note: Each element in the result&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[20,82,210],"class_list":["post-3137","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-array","tag-hashtable","tag-intersection","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=3137"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3137\/revisions"}],"predecessor-version":[{"id":3139,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/3137\/revisions\/3139"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=3137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=3137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=3137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}