LeetCode 338 - Counting Bits
Difficulty: easy
Problem Description
English (Counting Bits)
Given an integer n
, return an array ans
of length n + 1
such that for each i
(0 <= i <= n
), ans[i]
is the number of 1
‘s in the binary representation of i
.
Example 1:
1 |
|
Example 2:
1 |
|
Constraints:
0 <= n <= 10^5
Follow up:
- It is very easy to come up with a solution with a runtime of
O(n log n)
. Can you do it in linear timeO(n)
and possibly in a single pass? - Can you do it without using any built-in function (i.e., like
__builtin_popcount
in C++)?
Chinese (比特位计数)
给你一个整数 n
,对于 0 <= i <= n
中的每个 i
,计算其二进制表示中 1
的个数 ,返回一个长度为 n + 1
的数组 ans
作为答案。
示例 1:
1 |
|
示例 2:
1 |
|
提示:
0 <= n <= 10^5
进阶:
- 很容易就能实现时间复杂度为
O(n log n)
的解决方案,你可以在线性时间复杂度O(n)
内用一趟扫描解决此问题吗? - 你能不使用任何内置函数解决此问题吗?(如,C++ 中的
__builtin_popcount
)
Solution
Solution 1
1 |
|
Solution 2
1 |
|
LeetCode 338 - Counting Bits
http://wasprime.github.io/Algorithm/LeetCode/DynamicProgramming/LeetCode-338-Counting-Bits/