{"id":2232,"date":"2018-03-19T21:04:20","date_gmt":"2018-03-20T04:04:20","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2232"},"modified":"2018-03-19T21:08:19","modified_gmt":"2018-03-20T04:08:19","slug":"leetcode-461-hamming-distance","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-461-hamming-distance\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 461. Hamming Distance"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 461. Hamming Distance \u5237\u9898\u627e\u5de5\u4f5c EP3\" width=\"500\" height=\"281\" src=\"https:\/\/www.youtube.com\/embed\/RNaO23ETjhM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe><\/p>\n<h1><strong>Problem<\/strong><\/h1>\n<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 class=\"crayon:false\"><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<h1><strong>Solution: Bit Operation<\/strong><\/h1>\n<p>Time complexity: O(logn)<\/p>\n<p>Space complexity: O(1)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 6 ms\r\nclass Solution {\r\npublic:\r\n  int hammingDistance(int x, int y) {\r\n    int ans = 0;\r\n    int t = x^y;\r\n    while (t) {\r\n      ans += t &amp; 1;\r\n      t &gt;&gt;=1;\r\n    }\r\n    return ans;\r\n  }\r\n};<\/pre>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 9 ms\r\nclass Solution {\r\npublic:\r\n  int hammingDistance(int x, int y) {\r\n    return __builtin_popcount(x ^ y);\r\n  }\r\n};\r\n<\/pre>\n<h1><strong>Related Problems<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-477-total-hamming-distance\/\">\u82b1\u82b1\u9171 LeetCode 477. Total Hamming Distance<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Problem The\u00a0Hamming distance\u00a0between two integers is the number of positions at which the corresponding bits are different. Given two integers\u00a0x\u00a0and\u00a0y, calculate the Hamming distance. Note:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[126],"tags":[16,222,63],"class_list":["post-2232","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-easy","tag-xor","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2232","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=2232"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2232\/revisions"}],"predecessor-version":[{"id":2238,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2232\/revisions\/2238"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}