{"id":202,"date":"2018-08-20T17:30:19","date_gmt":"2018-08-20T09:30:19","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=202"},"modified":"2018-11-27T17:30:56","modified_gmt":"2018-11-27T09:30:56","slug":"461-hamming-distance","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=202","title":{"rendered":"461. Hamming Distance"},"content":{"rendered":"<p>The\u00a0<a href=\"https:\/\/en.wikipedia.org\/wiki\/Hamming_distance\" target=\"_blank\" rel=\"noopener\">Hamming distance<\/a>\u00a0between two integers is the number of positions at which the corresponding bits are different.<\/p>\n<p>Given two integers\u00a0<code>x<\/code>\u00a0and\u00a0<code>y<\/code>, calculate the Hamming distance.<\/p>\n<p><b>Note:<\/b><br \/>\n0 \u2264\u00a0<code>x<\/code>,\u00a0<code>y<\/code>\u00a0&lt; 2<sup>31<\/sup>.<\/p>\n<p><b>Example:<\/b><\/p>\n<pre><b>Input:<\/b> x = 1, y = 4\r\n\r\n<b>Output:<\/b> 2\r\n\r\n<b>Explanation:<\/b>\r\n1   (0 0 0 1)\r\n4   (0 1 0 0)\r\n       \u2191   \u2191\r\n\r\nThe above arrows point to positions where the corresponding bits are different.<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def hammingDistance(self, x, y):\r\n        &quot;&quot;&quot;\r\n        :type x: int\r\n        :type y: int\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        q = x^y\r\n        r = 0\r\n        for i in range(32):\r\n            r += (q&gt;&gt;i)&amp; 1\r\n        return r\r\n        \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The\u00a0Hamming distance\u00a0between two integers is the number<a href=\"https:\/\/www.ccagml.com\/?p=202\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">461. Hamming Distance<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/202"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=202"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":203,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/202\/revisions\/203"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}