友情支持
如果您觉得这个笔记对您有所帮助,看在D瓜哥码这么多字的辛苦上,请友情支持一下,D瓜哥感激不尽,😜
有些打赏的朋友希望可以加个好友,欢迎关注D 瓜哥的微信公众号,这样就可以通过公众号的回复直接给我发信息。
公众号的微信号是: jikerizhi 。因为众所周知的原因,有时图片加载不出来。 如果图片加载不出来可以直接通过搜索微信号来查找我的公众号。 |
338. Counting Bits
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.
Input: 2
Output: [0,1,1]
Input: 5
Output: [0,1,1,2,1,2]
Follow up:
-
It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
-
Space complexity should be O(n).
-
Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
解题分析
从 0
开始,每个奇数包含 1
的个数,都比前面一个数要多 1
。所以,只需要计算偶数包含的 1
的个数,然后奇数直接在前面的基础上加 1
即可。
另外一种解法:以 2
的 x
次幂为界,后面的的 x
个数中的 1
的个数,是前面个 x
个数在前面加 1
的结果(位数不够在前面补零,例如从 1
到 5
,就是从 1
补零 001
,再前面加 1
,则为 1001
)。
\begin{aligned} (0)&=(0)_{2}\\ (1)&=(1)_{2}\\ (2)&=(10)_{2}\\ (3)&=(11)_{2}\\ P(x+b)&=P(x)+1, b=2^{m}>x\\ \end{aligned}
for (int i = 0; i < 99; i++) {
int j = 0;
for (; Math.pow(2, j) <= i; j++) {
if (Math.pow(2, j) == i && i != 1) {
System.out.println("---");
}
}
System.out.printf("%2d %10s\n", i, Integer.toBinaryString(i));
}
用这段代码输出一下,观察一下更明显。
这个题的解法跟 89. Gray Code 有异曲同工之妙!
思考题:尝试"动态规划 + 最低有效位"的解法。
参考资料
Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1’s in their binary representation and return them as an array.
Example 1:
Input: 2
Output: [0,1,1]
Example 2:
Input: 5
Output:
[0,1,1,2,1,2]
Follow up:
-
It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
-
Space complexity should be O(n).
-
Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* Runtime: 1 ms, faster than 99.73% of Java online submissions for Counting Bits.
* Memory Usage: 43 MB, less than 5.88% of Java online submissions for Counting Bits.
* <p>
* Copy from: https://leetcode-cn.com/problems/counting-bits/solution/bi-te-wei-ji-shu-by-leetcode/[比特位计数 - 比特位计数 - 力扣(LeetCode)]
*/
public int[] countBits(int num) {
int[] result = new int[num + 1];
int b = 1;
int i = 0;
while (b <= num) {
while (i < b && i + b <= num) {
result[i + b] = result[i] + 1;
++i;
}
i = 0;
b <<= 1;
}
return result;
}
/**
* Runtime: 3 ms, faster than 24.83% of Java online submissions for Counting Bits.
* Memory Usage: 43 MB, less than 5.88% of Java online submissions for Counting Bits.
*/
public int[] countBitsEvenOdd(int num) {
int[] result = new int[num + 1];
for (int i = 0; i <= num; i += 2) {
result[i] = bitsInNum(i);
}
for (int i = 1; i <= num; i += 2) {
result[i] = result[i - 1] + 1;
}
return result;
}
private int bitsInNum(int num) {
int result = 0;
while (num != 0) {
if ((num & 1) == 1) {
result++;
}
num = num >>> 1;
}
return result;
}