mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-09-13 11:21:42 +08:00
add leetcode problem-cn part6
This commit is contained in:
49
算法题(国内版)/problem (Chinese)/比特位计数 [counting-bits].html
Normal file
49
算法题(国内版)/problem (Chinese)/比特位计数 [counting-bits].html
Normal file
@@ -0,0 +1,49 @@
|
||||
<p>给你一个整数 <code>n</code> ,对于 <code>0 <= i <= n</code> 中的每个 <code>i</code> ,计算其二进制表示中 <strong><code>1</code> 的个数</strong> ,返回一个长度为 <code>n + 1</code> 的数组 <code>ans</code> 作为答案。</p>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<div class="original__bRMd">
|
||||
<div>
|
||||
<p><strong>示例 1:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 2
|
||||
<strong>输出:</strong>[0,1,1]
|
||||
<strong>解释:</strong>
|
||||
0 --> 0
|
||||
1 --> 1
|
||||
2 --> 10
|
||||
</pre>
|
||||
|
||||
<p><strong>示例 2:</strong></p>
|
||||
|
||||
<pre>
|
||||
<strong>输入:</strong>n = 5
|
||||
<strong>输出:</strong>[0,1,1,2,1,2]
|
||||
<strong>解释:</strong>
|
||||
0 --> 0
|
||||
1 --> 1
|
||||
2 --> 10
|
||||
3 --> 11
|
||||
4 --> 100
|
||||
5 --> 101
|
||||
</pre>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li><code>0 <= n <= 10<sup>5</sup></code></li>
|
||||
</ul>
|
||||
|
||||
<p> </p>
|
||||
|
||||
<p><strong>进阶:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>很容易就能实现时间复杂度为 <code>O(n log n)</code> 的解决方案,你可以在线性时间复杂度 <code>O(n)</code> 内用一趟扫描解决此问题吗?</li>
|
||||
<li>你能不使用任何内置函数解决此问题吗?(如,C++ 中的 <code>__builtin_popcount</code> )</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
Reference in New Issue
Block a user