-
Notifications
You must be signed in to change notification settings - Fork 161
/
complement-of-base-10-integer.md
48 lines (35 loc) · 1.94 KB
/
complement-of-base-10-integer.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<p>Every non-negative integer <code>N</code> has a binary representation. For example, <code>5</code> can be represented as <code>"101"</code> in binary, <code>11</code> as <code>"1011"</code> in binary, and so on. Note that except for <code>N = 0</code>, there are no leading zeroes in any binary representation.</p>
<p>The <em>complement</em> of a binary representation is the number in binary you get when changing every <code>1</code> to a <code>0</code> and <code>0</code> to a <code>1</code>. For example, the complement of <code>"101"</code> in binary is <code>"010"</code> in binary.</p>
<p>For a given number <code>N</code> in base-10, return the complement of it's binary representation as a base-10 integer.</p>
<p> </p>
<ol>
</ol>
<div>
<p><strong>Example 1:</strong></p>
<pre>
<strong>Input: </strong><span id="example-input-1-1">5</span>
<strong>Output: </strong><span id="example-output-1">2</span>
<strong>Explanation: </strong>5 is "101" in binary, with complement "010" in binary, which is 2 in base-10.
</pre>
<div>
<p><strong>Example 2:</strong></p>
<pre>
<strong>Input: </strong><span id="example-input-2-1">7</span>
<strong>Output: </strong><span id="example-output-2">0</span>
<span id="example-output-1"><strong>Explanation: </strong>7 is "111" in binary, with complement "000" in binary, which is 0 in base-10.
</span></pre>
<div>
<p><strong>Example 3:</strong></p>
<pre>
<strong>Input: </strong><span id="example-input-3-1">10</span>
<strong>Output: </strong><span id="example-output-3">5</span>
<strong>Explanation: </strong>10 is "1010" in binary, with complement "0101" in binary, which is 5 in base-10.
</pre>
<p> </p>
<p><strong>Note:</strong></p>
<ol>
<li><code>0 <= N < 10^9</code></li>
</ol>
</div>
</div>
</div>