<?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>hamming Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/hamming/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/hamming/</link>
	<description></description>
	<lastBuildDate>Fri, 15 Dec 2017 05:10:42 +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>hamming Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/hamming/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<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>
