<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hashmap Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/hashmap/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/hashmap/</link>
	<description></description>
	<lastBuildDate>Fri, 22 Oct 2021 04:00:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>hashmap Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/hashmap/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2032. Two Out of Three</title>
		<link>https://zxi.mytechroad.com/blog/hashtable/leetcode-2032-two-out-of-three/</link>
					<comments>https://zxi.mytechroad.com/blog/hashtable/leetcode-2032-two-out-of-three/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 22 Oct 2021 03:55:16 +0000</pubDate>
				<category><![CDATA[Hashtable]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[hashmap]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8627</guid>

					<description><![CDATA[<p>Given three integer arrays&#160;nums1,&#160;nums2, and&#160;nums3, return&#160;a&#160;distinct&#160;array containing all the values that are present in&#160;at least two&#160;out of the three arrays. You may return the values&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2032-two-out-of-three/">花花酱 LeetCode 2032. Two Out of Three</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<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>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [1,1,3,2], nums2 = [2,3], nums3 = [3]
<strong>Output:</strong> [3,2]
<strong>Explanation:</strong> The values that are present in at least two arrays are:
- 3, in all three arrays.
- 2, in nums1 and nums2.
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [3,1], nums2 = [2,3], nums3 = [1,2]
<strong>Output:</strong> [2,3,1]
<strong>Explanation:</strong> The values that are present in at least two arrays are:
- 2, in nums2 and nums3.
- 3, in nums1 and nums2.
- 1, in nums1 and nums3.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums1 = [1,2,2], nums2 = [4,3,3], nums3 = [5]
<strong>Output:</strong> []
<strong>Explanation:</strong> No value is present in at least two arrays.
</pre>



<p><strong>Constraints:</strong></p>



<ul><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>



<h2><strong>Solution: Hashmap / Bitmask</strong></h2>



<p>s[x] := bitmask of x in all array[i]</p>



<p>s[x] = 101 =&gt; x in array0 and array2</p>



<p>Time complexity: O(n1 + n2 + n3)<br>Space complexity: O(n1 + n2 + n3)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; twoOutOfThree(vector&lt;int&gt;&amp; nums1, vector&lt;int&gt;&amp; nums2, vector&lt;int&gt;&amp; nums3) {
    unordered_map&lt;int, int&gt; s;    
    for (int x : nums1) s[x] |= 1;
    for (int x : nums2) s[x] |= 2;
    for (int x : nums3) s[x] |= 4;
    vector&lt;int&gt; ans;
    for (auto [x, v] : s)
      if (__builtin_popcount(v) &gt;= 2) ans.push_back(x);
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/hashtable/leetcode-2032-two-out-of-three/">花花酱 LeetCode 2032. Two Out of Three</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/hashtable/leetcode-2032-two-out-of-three/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 128. Longest Consecutive Sequence</title>
		<link>https://zxi.mytechroad.com/blog/zoj/leetcode-longest-consecutive-sequence/</link>
					<comments>https://zxi.mytechroad.com/blog/zoj/leetcode-longest-consecutive-sequence/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 16 Mar 2017 02:22:06 +0000</pubDate>
				<category><![CDATA[ZOJ]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hashmap]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=23</guid>

					<description><![CDATA[<p>Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/zoj/leetcode-longest-consecutive-sequence/">花花酱 LeetCode 128. Longest Consecutive Sequence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Given an unsorted array of integers, find the length of the longest consecutive elements sequence.</p>
<p>For example,<br />
Given <code>[100, 4, 200, 1, 3, 2]</code>,<br />
The longest consecutive elements sequence is <code>[1, 2, 3, 4]</code>. Return its length: <code>4</code>.</p>
<p>Your algorithm should run in O(<i>n</i>) complexity.</p><pre class="crayon-plain-tag">class Solution {
public:
    int longestConsecutive(vector&lt;int&gt; &amp;num) {
        unordered_map&lt;int,int&gt; m;
        int ans = 0;
        for(auto n : num) {
            if(m.count(n)) continue;
            if(m.count(n-1) &amp;&amp; m.count(n+1)){
                int l = m[n-1], r = m[n+1];
                m[n] = m[n-l] = m[n+r] = r+l+1;
            } else if(m.count(n-1)) {
                int l = m[n-1];
                m[n] = m[n-l] = l+1; 
            } else if(m.count(n+1)) {
                int r = m[n+1];
                m[n] = m[n+r] = r+1;
            } else {
                m[n] = 1;
            }
            ans = max(ans, m[n]);
        }
        return ans;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/zoj/leetcode-longest-consecutive-sequence/">花花酱 LeetCode 128. Longest Consecutive Sequence</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/zoj/leetcode-longest-consecutive-sequence/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
