<?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>mask Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/mask/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/mask/</link>
	<description></description>
	<lastBuildDate>Sun, 30 Apr 2023 15:17:54 +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>mask Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/mask/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2657. Find the Prefix Common Array of Two Arrays</title>
		<link>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2657-find-the-prefix-common-array-of-two-arrays/</link>
					<comments>https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2657-find-the-prefix-common-array-of-two-arrays/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 30 Apr 2023 15:10:21 +0000</pubDate>
				<category><![CDATA[Array]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[bitset]]></category>
		<category><![CDATA[mask]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[prefix]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=10027</guid>

					<description><![CDATA[<p>You are given two&#160;0-indexed&#160;integer&#160;permutations&#160;A&#160;and&#160;B&#160;of length&#160;n. A&#160;prefix common array&#160;of&#160;A&#160;and&#160;B&#160;is an array&#160;C&#160;such that&#160;C[i]&#160;is equal to the count of numbers that are present at or before the index&#160;i&#160;in&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2657-find-the-prefix-common-array-of-two-arrays/">花花酱 LeetCode 2657. Find the Prefix Common Array of Two Arrays</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>You are given two&nbsp;<strong>0-indexed&nbsp;</strong>integer<strong>&nbsp;</strong>permutations&nbsp;<code>A</code>&nbsp;and&nbsp;<code>B</code>&nbsp;of length&nbsp;<code>n</code>.</p>



<p>A&nbsp;<strong>prefix common array</strong>&nbsp;of&nbsp;<code>A</code>&nbsp;and&nbsp;<code>B</code>&nbsp;is an array&nbsp;<code>C</code>&nbsp;such that&nbsp;<code>C[i]</code>&nbsp;is equal to the count of numbers that are present at or before the index&nbsp;<code>i</code>&nbsp;in both&nbsp;<code>A</code>&nbsp;and&nbsp;<code>B</code>.</p>



<p>Return&nbsp;<em>the&nbsp;<strong>prefix common array</strong>&nbsp;of&nbsp;</em><code>A</code><em>&nbsp;and&nbsp;</em><code>B</code>.</p>



<p>A sequence of&nbsp;<code>n</code>&nbsp;integers is called a&nbsp;<strong>permutation</strong>&nbsp;if it contains all integers from&nbsp;<code>1</code>&nbsp;to&nbsp;<code>n</code>&nbsp;exactly once.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> A = [1,3,2,4], B = [3,1,2,4]
<strong>Output:</strong> [0,2,3,4]
<strong>Explanation:</strong> At i = 0: no number is common, so C[0] = 0.
At i = 1: 1 and 3 are common in A and B, so C[1] = 2.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
At i = 3: 1, 2, 3, and 4 are common in A and B, so C[3] = 4.
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> A = [2,3,1], B = [3,1,2]
<strong>Output:</strong> [0,1,3]
<strong>Explanation:</strong> At i = 0: no number is common, so C[0] = 0.
At i = 1: only 3 is common in A and B, so C[1] = 1.
At i = 2: 1, 2, and 3 are common in A and B, so C[2] = 3.
</pre>



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



<ul><li><code>1 &lt;= A.length == B.length == n &lt;= 50</code></li><li><code>1 &lt;= A[i], B[i] &lt;= n</code></li><li><code>It is guaranteed that A and B are both a permutation of n integers.</code></li></ul>



<h2><strong>Solution 1: Bitset</strong></h2>



<p>Use bitsets to store the numbers seen so far for each array, and use sA &amp; sB to count the common elements.</p>



<p>Time complexity: O(n*50)<br>Space complexity: O(50)</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; findThePrefixCommonArray(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {
    bitset&lt;51&gt; sA;
    bitset&lt;51&gt; sB;
    vector&lt;int&gt; ans;
    for (int i = 0; i &lt; A.size(); ++i) {
      sA.set(A[i]);
      sB.set(B[i]);
      ans.push_back((sA &amp; sB).count());
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Solution 2: Counter</strong></h2>



<p>Use a counter to track the frequency of each element, when the counter[x] == 2, we found a pair.</p>



<p>Time complexity: O(n)<br>Space complexity: O(n)</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; findThePrefixCommonArray(vector&lt;int&gt;&amp; A, vector&lt;int&gt;&amp; B) {
    vector&lt;int&gt; count(A.size() + 1);
    vector&lt;int&gt; ans;
    for (int i = 0, s = 0; i &lt; A.size(); ++i) {
      if (++count[A[i]] == 2) ++s;
      if (++count[B[i]] == 2) ++s;
      ans.push_back(s);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/algorithms/array/leetcode-2657-find-the-prefix-common-array-of-two-arrays/">花花酱 LeetCode 2657. Find the Prefix Common Array of Two Arrays</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/algorithms/array/leetcode-2657-find-the-prefix-common-array-of-two-arrays/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 137. Single Number II</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-137-single-number-ii/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-137-single-number-ii/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 28 Nov 2021 05:11:55 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[mask]]></category>
		<category><![CDATA[medium]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8846</guid>

					<description><![CDATA[<p>Given an integer array&#160;nums&#160;where&#160;every element appears&#160;three times&#160;except for one, which appears&#160;exactly once.&#160;Find the single element and return it. You must&#160;implement a solution with a linear&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-137-single-number-ii/">花花酱 LeetCode 137. Single Number II</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 integer array&nbsp;<code>nums</code>&nbsp;where&nbsp;every element appears&nbsp;<strong>three times</strong>&nbsp;except for one, which appears&nbsp;<strong>exactly once</strong>.&nbsp;<em>Find the single element and return it</em>.</p>



<p>You must&nbsp;implement a solution with a linear runtime complexity and use&nbsp;only constant&nbsp;extra space.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [2,2,3,2]
<strong>Output:</strong> 3
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [0,1,0,1,0,1,99]
<strong>Output:</strong> 99
</pre>



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



<ul><li><code>1 &lt;= nums.length &lt;= 3 * 10<sup>4</sup></code></li><li><code>-2<sup>31</sup>&nbsp;&lt;= nums[i] &lt;= 2<sup>31</sup>&nbsp;- 1</code></li><li>Each element in&nbsp;<code>nums</code>&nbsp;appears exactly&nbsp;<strong>three times</strong>&nbsp;except for one element which appears&nbsp;<strong>once</strong>.</li></ul>



<h2><strong>Solution: Bit by bit</strong></h2>



<p>Since every number appears three times, the i-th bit must be a factor of 3, if not, that bit belongs to the single number.</p>



<p>Time complexity: O(32n)<br>Space complexity: O(1)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int singleNumber(vector&lt;int&gt;&amp; nums) {
    int ans = 0;
    for (int s = 0; s &lt; 32; ++s) {
      int mask = 1 &lt;&lt; s;
      int sum = 0;
      for (int x : nums)
        if (x &amp; mask) ++sum;
      if (sum % 3) ans |= mask;
    }
    return ans;
  }
};</pre>
</div></div>



<h2><strong>Related Problems</strong></h2>



<ul><li><a href="https://zxi.mytechroad.com/blog/bit/leetcode-136-single-number/" data-type="post" data-id="8843">花花酱 LeetCode 136. Single Number</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-137-single-number-ii/">花花酱 LeetCode 137. Single Number II</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/bit/leetcode-137-single-number-ii/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1799. Maximize Score After N Operations</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1799-maximize-score-after-n-operations/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1799-maximize-score-after-n-operations/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sat, 20 Mar 2021 16:47:45 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[bitmap]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[mask]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=8259</guid>

					<description><![CDATA[<p>You are given&#160;nums, an array of positive integers of size&#160;2 * n. You must perform&#160;n&#160;operations on this array. In the&#160;ith&#160;operation&#160;(1-indexed), you will: Choose two elements,&#160;x&#160;and&#160;y.&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1799-maximize-score-after-n-operations/">花花酱 LeetCode 1799. Maximize Score After N Operations</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 1799. Maximize Score After N Operations - 刷题找工作 EP390" width="500" height="281" src="https://www.youtube.com/embed/M-rv0PV_NnE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given&nbsp;<code>nums</code>, an array of positive integers of size&nbsp;<code>2 * n</code>. You must perform&nbsp;<code>n</code>&nbsp;operations on this array.</p>



<p>In the&nbsp;<code>i<sup>th</sup></code>&nbsp;operation&nbsp;<strong>(1-indexed)</strong>, you will:</p>



<ul><li>Choose two elements,&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>.</li><li>Receive a score of&nbsp;<code>i * gcd(x, y)</code>.</li><li>Remove&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>&nbsp;from&nbsp;<code>nums</code>.</li></ul>



<p>Return&nbsp;<em>the maximum score you can receive after performing&nbsp;</em><code>n</code><em>&nbsp;operations.</em></p>



<p>The function&nbsp;<code>gcd(x, y)</code>&nbsp;is the greatest common divisor of&nbsp;<code>x</code>&nbsp;and&nbsp;<code>y</code>.</p>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong>&nbsp;The optimal choice of operations is:
(1 * gcd(1, 2)) = 1
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [3,4,6,8]
<strong>Output:</strong> 11
<strong>Explanation:</strong>&nbsp;The optimal choice of operations is:
(1 * gcd(3, 6)) + (2 * gcd(4, 8)) = 3 + 8 = 11
</pre>



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



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> nums = [1,2,3,4,5,6]
<strong>Output:</strong> 14
<strong>Explanation:</strong>&nbsp;The optimal choice of operations is:
(1 * gcd(1, 5)) + (2 * gcd(2, 4)) + (3 * gcd(3, 6)) = 1 + 4 + 9 = 14
</pre>



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



<ul><li><code>1 &lt;= n &lt;= 7</code></li><li><code>nums.length == 2 * n</code></li><li><code>1 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li></ul>



<h2><strong>Solution: Mask DP</strong></h2>



<figure class="wp-block-image size-large"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/03/1799-ep390.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/03/1799-ep390.png" alt="" class="wp-image-8277" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2021/03/1799-ep390.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/03/1799-ep390-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2021/03/1799-ep390-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<p>dp(mask, i) := max score of numbers (represented by a binary mask) at the i-th operations.<br>ans = dp(1, mask)<br>base case: dp = 0 if mask == 0<br>Transition: dp(mask, i) = max(dp(new_mask, i + 1) + i * gcd(nums[m], nums[n]))<br></p>



<p>Time complexity: O(n<sup>2</sup>*2<sup>2n</sup>)<br>Space complexity: O(2<sup>2n</sup>)</p>



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

<pre class="crayon-plain-tag">// Author: Huahua, 184 ms, 8.3 MB
class Solution {
public:
  int maxScore(vector&lt;int&gt;&amp; nums) {
    const int l = nums.size();
    vector&lt;int&gt; cache(1 &lt;&lt; l);
    function&lt;int(int)&gt; dp = [&amp;](int mask) {      
      if (mask == 0) return 0;
      int&amp; ans = cache[mask];
      if (ans &gt; 0) return ans;
      const int k = (l - __builtin_popcount(mask)) / 2 + 1;
      for (int i = 0; i &lt; l; ++i)
        for (int j = i + 1; j &lt; l; ++j)
          if ((mask &amp; (1 &lt;&lt; i)) &amp;&amp; (mask &amp; (1 &lt;&lt; j)))
            ans = max(ans, k * gcd(nums[i], nums[j])
                           + dp(mask ^ (1 &lt;&lt; i) ^ (1 &lt;&lt; j)));
      return ans;
    };
    return dp((1 &lt;&lt; l) - 1);
  }
};</pre>
</div></div>



<p>Bottom-Up</p>



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

<pre class="crayon-plain-tag">// Author: Huahua, 156 ms, 8.3 MB
class Solution {
public:
  int maxScore(vector&lt;int&gt;&amp; nums) {
    const int l = nums.size();
    vector&lt;int&gt; dp(1 &lt;&lt; l);    
    for (int mask = 0; mask &lt; 1 &lt;&lt; l; ++mask) {
      int c = __builtin_popcount(mask);
      if (c &amp; 1) continue; // only do in pairs
      int k = c / 2 + 1;
      for (int i = 0; i &lt; l; ++i)
        for (int j = i + 1; j &lt; l; ++j)
          if ((mask &amp; (1 &lt;&lt; i)) + (mask &amp; (1 &lt;&lt; j)) == 0) {            
            int new_mask = mask | (1 &lt;&lt; i) | (1 &lt;&lt; j);            
            dp[new_mask] = max(dp[new_mask],
                               k * gcd(nums[i], nums[j]) + dp[mask]);
        }
    }
    return dp[(1 &lt;&lt; l) - 1];
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-1799-maximize-score-after-n-operations/">花花酱 LeetCode 1799. Maximize Score After N Operations</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/dynamic-programming/leetcode-1799-maximize-score-after-n-operations/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 477. Total Hamming Distance</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-477-total-hamming-distance/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-477-total-hamming-distance/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Fri, 15 Dec 2017 02:36:14 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[distance]]></category>
		<category><![CDATA[hamming]]></category>
		<category><![CDATA[mask]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1243</guid>

					<description><![CDATA[<p>Problem: The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-477-total-hamming-distance/">花花酱 LeetCode 477. Total Hamming Distance</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="question-description">
<p><iframe width="500" height="375" src="https://www.youtube.com/embed/fH9clXXrS2Q?feature=oembed" frameborder="0" gesture="media" allow="encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank" rel="noopener">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.</p>
<p>Now your job is to find the total Hamming distance between all pairs of the given numbers.</p>
<p><b>Example:</b></p><pre class="crayon-plain-tag">Input: 4, 14, 2

Output: 6

Explanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just
showing the four bits relevant in this case). So the answer will be:
HammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.</pre><p><b>Note:</b></p>
<ol>
<li>Elements of the given array are in the range of <code>0 </code>to <code>10^9</code></li>
<li>Length of the array will not exceed <code>10^4</code>.</li>
</ol>
<p><strong>题目大意：</strong>给你一堆数，让你求所有数对的HammingDistance的总和。</p>
<p><strong>Idea:</strong></p>
<ol>
<li>Brute force, compute HammingDistance for all pairs. O(n^2) TLE</li>
<li>Count how many ones on i-th bit, assuming k. Distance += k * (n &#8211; k). O(n)</li>
</ol>
<p><img class="alignnone size-full wp-image-1248" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/477-ep132.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/477-ep132.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/477-ep132-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/12/477-ep132-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></p>
</div>
<p><strong>Solution:</strong></p>
<p>C++ / O(n)</p><pre class="crayon-plain-tag">// Author: Huahua
// Runtime: 59 ms
class Solution {
public:
    int totalHammingDistance(vector&lt;int&gt;&amp; nums) {
        int ans = 0;
        int mask = 1;
        for (int i = 0; i &lt; 32; ++i) {
            int count = 0;
            for (const int num : nums)
                if (num &amp; mask) ++count;
            ans += (nums.size() - count) * count;
            mask &lt;&lt;= 1;
        }
        return ans;
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-477-total-hamming-distance/">花花酱 LeetCode 477. Total Hamming Distance</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/bit/leetcode-477-total-hamming-distance/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
