剑指 Offer II 003:前 n 个数字二进制中 1 的个数
This commit is contained in:
parent
61ddc5b0d6
commit
0ae70e9e4c
76
src/main/java/leetcode/editor/cn/W3tCBm.java
Normal file
76
src/main/java/leetcode/editor/cn/W3tCBm.java
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
//给定一个非负整数 n ,请计算 0 到 n 之间的每个数字的二进制表示中 1 的个数,并输出一个数组。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: n = 2
|
||||||
|
//输出: [0,1,1]
|
||||||
|
//解释:
|
||||||
|
//0 --> 0
|
||||||
|
//1 --> 1
|
||||||
|
//2 --> 10
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入: n = 5
|
||||||
|
//输出: [0,1,1,2,1,2]
|
||||||
|
//解释:
|
||||||
|
//0 --> 0
|
||||||
|
//1 --> 1
|
||||||
|
//2 --> 10
|
||||||
|
//3 --> 11
|
||||||
|
//4 --> 100
|
||||||
|
//5 --> 101
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 说明 :
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 0 <= n <= 10⁵
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 进阶:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 给出时间复杂度为 O(n*sizeof(integer)) 的解答非常容易。但你可以在线性时间 O(n) 内用一趟扫描做到吗?
|
||||||
|
// 要求算法的空间复杂度为 O(n) 。
|
||||||
|
// 你能进一步完善解法吗?要求在C++或任何其他语言中不使用任何内置函数(如 C++ 中的 __builtin_popcount )来执行此操作。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 注意:本题与主站 338 题相同:https://leetcode-cn.com/problems/counting-bits/
|
||||||
|
// Related Topics 位运算 动态规划 👍 28 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
//剑指 Offer II 003:前 n 个数字二进制中 1 的个数
|
||||||
|
public class W3tCBm {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Solution solution = new W3tCBm().new Solution();
|
||||||
|
// TO TEST
|
||||||
|
}
|
||||||
|
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public int[] countBits(int n) {
|
||||||
|
int[] nums = new int[n + 1];
|
||||||
|
for (int i = 0; i <= n; i++) {
|
||||||
|
for (int j = 0; j < 32; j++) {
|
||||||
|
nums[i] += (i >> j) & 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nums;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
File diff suppressed because one or more lines are too long
51
src/main/java/leetcode/editor/cn/doc/content/W3tCBm.md
Normal file
51
src/main/java/leetcode/editor/cn/doc/content/W3tCBm.md
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
<p>给定一个非负整数 <code>n</code><b> </b>,请计算 <code>0</code> 到 <code>n</code> 之间的每个数字的二进制表示中 1 的个数,并输出一个数组。</p>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入: </strong>n =<strong> </strong>2
|
||||||
|
<strong>输出: </strong>[0,1,1]
|
||||||
|
<strong>解释:
|
||||||
|
</strong>0 --> 0
|
||||||
|
1 --> 1
|
||||||
|
2 --> 10
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
|
||||||
|
<pre>
|
||||||
|
<strong>输入: </strong>n =<strong> </strong>5
|
||||||
|
<strong>输出: </strong><code>[0,1,1,2,1,2]
|
||||||
|
</code><span style="white-space: pre-wrap;"><strong>解释:</strong>
|
||||||
|
</span>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*sizeof(integer))</code><strong> </strong>的解答非常容易。但你可以在线性时间 <code>O(n)</code><strong> </strong>内用一趟扫描做到吗?</li>
|
||||||
|
<li>要求算法的空间复杂度为 <code>O(n)</code> 。</li>
|
||||||
|
<li>你能进一步完善解法吗?要求在C++或任何其他语言中不使用任何内置函数(如 C++ 中的 <code>__builtin_popcount</code><strong> </strong>)来执行此操作。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><meta charset="UTF-8" />注意:本题与主站 338 题相同:<a href="https://leetcode-cn.com/problems/counting-bits/">https://leetcode-cn.com/problems/counting-bits/</a></p>
|
||||||
|
<div><div>Related Topics</div><div><li>位运算</li><li>动态规划</li></div></div><br><div><li>👍 28</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user