{"id":8627,"date":"2021-10-21T20:55:16","date_gmt":"2021-10-22T03:55:16","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8627"},"modified":"2021-10-21T21:00:49","modified_gmt":"2021-10-22T04:00:49","slug":"leetcode-2032-two-out-of-three","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/hashtable\/leetcode-2032-two-out-of-three\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2032. Two Out of Three"},"content":{"rendered":"\n<p>Given three integer arrays&nbsp;<code>nums1<\/code>,&nbsp;<code>nums2<\/code>, and&nbsp;<code>nums3<\/code>, return&nbsp;<em>a&nbsp;<strong>distinct<\/strong>&nbsp;array containing all the values that are present in&nbsp;<strong>at least two<\/strong>&nbsp;out of the three arrays. You may return the values in&nbsp;<strong>any<\/strong>&nbsp;order<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]\n<strong>Output:<\/strong> [3,2]\n<strong>Explanation:<\/strong> The values that are present in at least two arrays are:\n- 3, in all three arrays.\n- 2, in nums1 and nums2.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]\n<strong>Output:<\/strong> [2,3,1]\n<strong>Explanation:<\/strong> The values that are present in at least two arrays are:\n- 2, in nums2 and nums3.\n- 3, in nums1 and nums2.\n- 1, in nums1 and nums3.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]\n<strong>Output:<\/strong> []\n<strong>Explanation:<\/strong> No value is present in at least two arrays.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums1.length, nums2.length, nums3.length &lt;= 100<\/code><\/li><li><code>1 &lt;= nums1[i], nums2[j], nums3[k] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Hashmap \/ Bitmask<\/strong><\/h2>\n\n\n\n<p>s[x] := bitmask of x in all array[i]<\/p>\n\n\n\n<p>s[x] = 101 =&gt; x in array0 and array2<\/p>\n\n\n\n<p>Time complexity: O(n1 + n2 + n3)<br>Space complexity: O(n1 + n2 + n3)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {\n    unordered_map<int, int> s;    \n    for (int x : nums1) s[x] |= 1;\n    for (int x : nums2) s[x] |= 2;\n    for (int x : nums3) s[x] |= 4;\n    vector<int> ans;\n    for (auto [x, v] : s)\n      if (__builtin_popcount(v) >= 2) ans.push_back(x);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given three integer arrays&nbsp;nums1,&nbsp;nums2, and&nbsp;nums3, return&nbsp;a&nbsp;distinct&nbsp;array containing all the values that are present in&nbsp;at least two&nbsp;out of the three arrays. You may return the values&#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":[16,222,19],"class_list":["post-8627","post","type-post","status-publish","format-standard","hentry","category-hashtable","tag-bit","tag-easy","tag-hashmap","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8627","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=8627"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8627\/revisions"}],"predecessor-version":[{"id":8629,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8627\/revisions\/8629"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8627"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8627"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8627"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}