{"id":4974,"date":"2019-03-16T23:29:32","date_gmt":"2019-03-17T06:29:32","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4974"},"modified":"2019-03-16T23:31:22","modified_gmt":"2019-03-17T06:31:22","slug":"leetcode-1012-complement-of-base-10-integer","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/bit\/leetcode-1012-complement-of-base-10-integer\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1012. Complement of Base 10 Integer"},"content":{"rendered":"\n<p>Every non-negative integer&nbsp;<code>N<\/code>&nbsp;has a binary representation.&nbsp; For example,&nbsp;<code>5<\/code>&nbsp;can be represented as&nbsp;<code>\"101\"<\/code>&nbsp;in binary,&nbsp;<code>11<\/code>&nbsp;as&nbsp;<code>\"1011\"<\/code>&nbsp;in binary, and so on.&nbsp; Note that except for&nbsp;<code>N = 0<\/code>, there are no leading zeroes in any&nbsp;binary representation.<\/p>\n\n\n\n<p>The&nbsp;<em>complement<\/em>&nbsp;of a binary representation&nbsp;is the number in binary you get when changing every&nbsp;<code>1<\/code>&nbsp;to a&nbsp;<code>0<\/code>&nbsp;and&nbsp;<code>0<\/code>&nbsp;to a&nbsp;<code>1<\/code>.&nbsp; For example, the complement of&nbsp;<code>\"101\"<\/code>&nbsp;in binary is&nbsp;<code>\"010\"<\/code>&nbsp;in binary.<\/p>\n\n\n\n<p>For a given number&nbsp;<code>N<\/code>&nbsp;in base-10, return the complement of it&#8217;s binary representation as a&nbsp;base-10 integer.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>5\n<strong>Output: <\/strong>2\n<strong>Explanation: <\/strong>5 is \"101\" in binary, with complement \"010\" in binary, which is 2 in base-10.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>7\n<strong>Output: <\/strong>0\n<strong>Explanation: <\/strong>7 is \"111\" in binary, with complement \"000\" in binary, which is 0 in base-10.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted crayon:false\"><strong>Input: <\/strong>10\n<strong>Output: <\/strong>5\n<strong>Explanation: <\/strong>10 is \"1010\" in binary, with complement \"0101\" in binary, which is 5 in base-10.\n<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><code>0 &lt;= N &lt; 10^9<\/code><\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Bit<\/strong><\/h2>\n\n\n\n<p>Find the smallest binary number c that is all 1s, (e.g. &#8220;111&#8221;, &#8220;11111&#8221;) that is greater or equal to N.<br>ans = C ^ N or C &#8211; N<\/p>\n\n\n\n<p>Time complexity: O(log(n))<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua, running time: 4 ms, 8 MB\nclass Solution {\npublic:\n  int bitwiseComplement(int N) {\n    int c = 1;\n    while (c < N) \n      c = (c << 1) | 1;\n    return N ^ c;    \n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Every non-negative integer&nbsp;N&nbsp;has a binary representation.&nbsp; For example,&nbsp;5&nbsp;can be represented as&nbsp;&#8220;101&#8221;&nbsp;in binary,&nbsp;11&nbsp;as&nbsp;&#8220;1011&#8221;&nbsp;in binary, and so on.&nbsp; Note that except for&nbsp;N = 0, there are no&#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,459],"class_list":["post-4974","post","type-post","status-publish","format-standard","hentry","category-bit","tag-bit","tag-easy","tag-ologn","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4974","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=4974"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4974\/revisions"}],"predecessor-version":[{"id":4976,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4974\/revisions\/4976"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}