{"id":186,"date":"2018-05-26T21:22:44","date_gmt":"2018-05-26T13:22:44","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=186"},"modified":"2018-11-27T17:23:20","modified_gmt":"2018-11-27T09:23:20","slug":"191-number-of-1-bits","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=186","title":{"rendered":"191. Number of 1 Bits"},"content":{"rendered":"<p>Write a function that takes an unsigned integer and returns the number of &#8216;1&#8217;\u00a0bits it has (also known as the\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Hamming_weight\" target=\"_blank\" rel=\"noopener\">Hamming weight<\/a>).<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre><strong>Input:<\/strong> 11\r\n<strong>Output:<\/strong> 3\r\n<strong>Explanation: <\/strong>Integer 11 has binary representation <code><strong>00000000000000000000000000001011 <\/strong><\/code><\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre><strong>Input:<\/strong> 128\r\n<strong>Output:<\/strong> 1\r\n<strong>Explanation: <\/strong>Integer 128 has binary representation <strong>00000000000000000000000010000000<\/strong><\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def hammingWeight(self, n):\r\n        &quot;&quot;&quot;\r\n        :type n: int\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        a = str(bin(n)).replace(&quot;b&quot;, &quot;&quot;)\r\n        result = 0\r\n        for i in a:\r\n            result += int(i)\r\n        return result\r\n        \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Write a function that takes an unsigned integer and ret<a href=\"https:\/\/www.ccagml.com\/?p=186\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">191. Number of 1 Bits<\/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\/186"}],"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=186"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":187,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions\/187"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}