Skip to content

Commit 3936f11

Browse files
committed
338 Counting Bits
1 parent 7e24b63 commit 3936f11

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
+ [237 Delete Node in a Linked List(O(1)删除单链表节点)](algorithms/DeleteNodeinaLinkedList)
139139
+ [238 Product of Array Except Self(数组操作)](algorithms/ProductofArrayExceptSelf)
140140
+ [242 Valid Anagram(hash表,判断两个字符串出现的字符和次数均相同)](algorithms/ValidAnagram)
141+
+ [338 Counting Bits(二进制1的个数,迭代法)](algorithms/CountingBits)
141142

142143
## Database
143144

algorithms/CountingBits/README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Counting Bits
2+
3+
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.
4+
5+
Example:
6+
For `num = 5` you should return `[0,1,1,2,1,2]`.
7+
8+
Follow up:
9+
10+
* 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?
11+
* Space complexity should be O(n).
12+
* Can you do it like a boss? Do it without using any builtin function like\_\_builtin\_popcount in c++ or in any other language.
13+
14+
## Solution
15+
16+
很容易实现O(n * sizeof(num))的方法:
17+
18+
```c
19+
int countBit(int n)
20+
{
21+
int sum = 0;
22+
while (n) {
23+
sum++;
24+
n &= (n - 1);
25+
}
26+
return sum;
27+
}
28+
int *countBits(int num, int *size)
29+
{
30+
*size = num + 1;
31+
int* ans = (int *)malloc(sizeof(int) * *size);
32+
for (int i = 0; i <= num; ++i)
33+
ans[i] = countBit(i);
34+
return ans;
35+
}
36+
```
37+
38+
但题目要求O(n),我们可以使用迭代的方法,我们知道左移位除了最后一个1可能丢掉,不会改变其余位的1的个数,假设f(n)表示n的二进制1的个数,则:
39+
40+
* 若n为偶数,则f(n) = f(n >> 1).
41+
* 若n为奇数,则f(n) = f (n >> 1) + 1. 其中后面的1是由于移位丢掉的。
42+
43+
```c
44+
int *countBits(int num, int *size)
45+
{
46+
*size = num + 1;
47+
int* ans = (int *)malloc(sizeof(int) * *size);
48+
for (int i = 1; i <= num; ++i)
49+
ans[i] = ans[i >> 1] + (i & 1);
50+
return ans;
51+
}
52+
```

algorithms/CountingBits/solve.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
class Solution {
4+
public:
5+
vector<int> countBits(int num) {
6+
vector<int> result(num + 1, 0);
7+
for (int i = 1; i <= num; ++i)
8+
result[i] = result[i >> 1] + (i & 1);
9+
return result;
10+
}
11+
};
12+
int countBit(int n)
13+
{
14+
int sum = 0;
15+
while (n) {
16+
sum++;
17+
n &= (n - 1);
18+
}
19+
return sum;
20+
}
21+
int *countBits_bad(int num, int *size)
22+
{
23+
*size = num + 1;
24+
int* ans = (int *)malloc(sizeof(int) * *size);
25+
for (int i = 0; i <= num; ++i)
26+
ans[i] = countBit(i);
27+
return ans;
28+
}
29+
int *countBits(int num, int *size)
30+
{
31+
*size = num + 1;
32+
int* ans = (int *)malloc(sizeof(int) * *size);
33+
for (int i = 1; i <= num; ++i)
34+
ans[i] = ans[i >> 1] + (i & 1);
35+
return ans;
36+
}
37+
int main(int argc, char **argv)
38+
{
39+
int n;
40+
while (scanf("%d", &n) != EOF) {
41+
int size = 0;
42+
int *ans = countBits(n, &size);
43+
for (int i = 0; i < size; ++i) {
44+
printf("%d ", ans[i]);
45+
}
46+
printf("\n");
47+
free(ans);
48+
}
49+
return 0;
50+
}

0 commit comments

Comments
 (0)