mirror of
				https://gitee.com/coder-xiaomo/leetcode-problemset
				synced 2025-11-04 03:33:12 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			41 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			41 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<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> </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> </p>
 | 
						||
 | 
						||
<p><strong>提示:</strong></p>
 | 
						||
 | 
						||
<ul>
 | 
						||
	<li><code>1 <= num < 2<sup>31</sup></code></li>
 | 
						||
</ul>
 | 
						||
 | 
						||
<p> </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>
 |