338. 比特位计数
题目描述
给你一个整数 n
,对于 0 <= i <= n
中的每个 i
,计算其二进制表示中 1
的个数 ,返回一个长度为 n + 1
的数组 ans
作为答案。
示例 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 <= 105
进阶:
- 很容易就能实现时间复杂度为
O(n log n)
的解决方案,你可以在线性时间复杂度O(n)
内用一趟扫描解决此问题吗? - 你能不使用任何内置函数解决此问题吗?(如,C++ 中的
__builtin_popcount
)
解法
方法一:位运算
我们直接枚举 $0 \leq i \leq n$ 中的每个数,对于每个数 $i$,我们用库函数或者 $lowbit$ 运算得到 $i$ 中二进制位 $1$ 的个数。
时间复杂度 $O(n \times \log n)$,忽略答案的空间消耗,空间复杂度 $O(1)$。
Python3
class Solution:
def countBits(self, n: int) -> List[int]:
return [i.bit_count() for i in range(n + 1)]
Java
class Solution {
public int[] countBits(int n) {
int[] ans = new int[n + 1];
for (int i = 0; i <= n; ++i) {
ans[i] = Integer.bitCount(i);
}
return ans;
}
}
C++
class Solution {
public:
vector<int> countBits(int n) {
vector<int> ans(n + 1);
for (int i = 0; i <= n; ++i) {
ans[i] = __builtin_popcount(i);
}
return ans;
}
};
Go
func countBits(n int) []int {
ans := make([]int, n+1)
for i := 0; i <= n; i++ {
ans[i] = bits.OnesCount(uint(i))
}
return ans
}
TypeScript
function countBits(n: number): number[] {
const ans: number[] = Array(n + 1).fill(0);
for (let i = 0; i <= n; ++i) {
ans[i] = bitCount(i);
}
return ans;
}
function bitCount(n: number): number {
let count = 0;
while (n) {
n &= n - 1;
++count;
}
return count;
}
方法二:动态规划
我们定义一个长度为 $n+1$ 的答案数组 $ans$,初始时 $ans[0]=0$。
对于 $1 \leq i \leq n$,我们有 $ans[i] = ans[i \textit{ and } (i-1)] + 1$。其中 $i \textit{ and } (i-1)$ 表示将 $i$ 的二进制表示中的最低位 $1$ 变成 $0$ 之后的数,显然 $i \textit{ and } (i-1) < i$,且 $ans[i \textit{ and } (i-1)]$ 已经被计算出来了,我们就能以 $O(1)$ 的时间得到 $ans[i]$。
时间复杂度 $O(n)$,空间复杂度 $O(1)$。
Python3
class Solution:
def countBits(self, n: int) -> List[int]:
ans = [0] * (n + 1)
for i in range(1, n + 1):
ans[i] = ans[i & (i - 1)] + 1
return ans
Java
class Solution {
public int[] countBits(int n) {
int[] ans = new int[n + 1];
for (int i = 1; i <= n; ++i) {
ans[i] = ans[i & (i - 1)] + 1;
}
return ans;
}
}
C++
class Solution {
public:
vector<int> countBits(int n) {
vector<int> ans(n + 1);
for (int i = 1; i <= n; ++i) {
ans[i] = ans[i & (i - 1)] + 1;
}
return ans;
}
};
Go
func countBits(n int) []int {
ans := make([]int, n+1)
for i := 1; i <= n; i++ {
ans[i] = ans[i&(i-1)] + 1
}
return ans
}
TypeScript
function countBits(n: number): number[] {
const ans: number[] = Array(n + 1).fill(0);
for (let i = 1; i <= n; ++i) {
ans[i] = ans[i & (i - 1)] + 1;
}
return ans;
}