{"id":1366,"date":"2017-12-29T23:12:21","date_gmt":"2017-12-30T07:12:21","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1366"},"modified":"2019-09-29T21:42:08","modified_gmt":"2019-09-30T04:42:08","slug":"476-number-complement","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/476-number-complement\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 476. Number Complement"},"content":{"rendered":"<p><iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 476. Number Complement - \u5237\u9898\u627e\u5de5\u4f5c EP143\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/LZtFS4ple3c?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<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u8f93\u51fa\u548c\u5b83\u4e92\u8865\u7684\u6570\uff08\u7ffb\u8f6c\u6240\u6709\u7684bits\uff09\u3002<\/p>\n<p><strong>Problem:<\/strong><\/p>\n<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 class=\"\">Input: 5\nOutput: 2\nExplanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.\n<\/pre>\n<p><b>Example 2:<\/b><\/p>\n<pre class=\"\">Input: 1\nOutput: 0\nExplanation: 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>Idea:<\/strong><\/p>\n<p>Bit<\/p>\n<p><script async=\"\" src=\"https:\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br \/>\n<ins class=\"adsbygoogle\" style=\"display:block\" data-ad-format=\"fluid\" data-ad-layout-key=\"-fb+5w+4e-db+86\" data-ad-client=\"ca-pub-2404451723245401\" data-ad-slot=\"2162692788\"><\/ins><br \/>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n<p><strong>Solution:<\/strong><\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\n\/\/ Running time: 3 ms\nclass Solution {\npublic:\n    int findComplement(int num) {        \n        int mask = ~0;        \n        while (num &amp; mask) mask &lt;&lt;= 1;\n        return ~num ^ mask;\n    }\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u7ed9\u4f60\u4e00\u4e2a\u6b63\u6574\u6570\uff0c\u8f93\u51fa\u548c\u5b83\u4e92\u8865\u7684\u6570\uff08\u7ffb\u8f6c\u6240\u6709\u7684bits\uff09\u3002 Problem: Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation. Note: The given&#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,163],"tags":[16],"class_list":["post-1366","post","type-post","status-publish","format-standard","hentry","category-bit","category-easy","tag-bit","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1366","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=1366"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1366\/revisions"}],"predecessor-version":[{"id":5638,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1366\/revisions\/5638"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}