{"id":204,"date":"2018-08-20T17:35:00","date_gmt":"2018-08-20T09:35:00","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=204"},"modified":"2018-11-27T17:31:45","modified_gmt":"2018-11-27T09:31:45","slug":"476-number-complement","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=204","title":{"rendered":"476. Number Complement"},"content":{"rendered":"<p>Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.<\/p>\n<p><b>Note:<\/b><\/p>\n<ol>\n<li>The given integer is guaranteed to fit within the range of a 32-bit signed integer.<\/li>\n<li>You could assume no leading zero bit in the integer\u2019s binary representation.<\/li>\n<\/ol>\n<p><b>Example 1:<\/b><\/p>\n<pre><b>Input:<\/b> 5\r\n<b>Output:<\/b> 2\r\n<b>Explanation:<\/b> The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.\r\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre><b>Input:<\/b> 1\r\n<b>Output:<\/b> 0\r\n<b>Explanation:<\/b> The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.<\/pre>\n<p><strong>Python<\/strong><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\"> \r\nclass Solution(object):\r\n    def findComplement(self, num):\r\n        &quot;&quot;&quot;\r\n        :type num: int\r\n        :rtype: int\r\n        &quot;&quot;&quot;\r\n        b = num\r\n        index = 0\r\n        while b &gt; 0:\r\n            b = b &gt;&gt; 1\r\n            index += 1\r\n        return num ^ (2 ** index - 1)\r\n        \r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Given a positive integer, output its complement number.<a href=\"https:\/\/www.ccagml.com\/?p=204\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">476. Number Complement<\/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\/204"}],"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=204"}],"version-history":[{"count":1,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions"}],"predecessor-version":[{"id":205,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/204\/revisions\/205"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}