1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/数字的补数 [number-complement].html
2022-03-29 12:43:11 +08:00

41 lines
1.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>对整数的二进制表示取反(<code>0</code><code>1</code> <code>1</code><code>0</code>)后,再转换为十进制表示,可以得到这个整数的补数。</p>
<ul>
<li>例如,整数 <code>5</code> 的二进制表示是 <code>"101"</code> ,取反后得到 <code>"010"</code> ,再转回十进制表示得到补数 <code>2</code></li>
</ul>
<p>给你一个整数 <code>num</code> ,输出它的补数。</p>
<p>&nbsp;</p>
<ol>
</ol>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>num = 5
<strong>输出:</strong>2
<strong>解释:</strong>5 的二进制表示为 101没有前导零位其补数为 010。所以你需要输出 2 。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>num = 1
<strong>输出:</strong>0
<strong>解释:</strong>1 的二进制表示为 1没有前导零位其补数为 0。所以你需要输出 0 。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= num &lt; 2<sup>31</sup></code></li>
</ul>
<p>&nbsp;</p>
<p><strong>注意:</strong>本题与 1009 <a href="https://leetcode-cn.com/problems/complement-of-base-10-integer/">https://leetcode-cn.com/problems/complement-of-base-10-integer/</a> 相同</p>