1. Title
Given a non-negative integer num . For each digit i in the range of 0 ≤ i ≤ num , count the number of 1s in its binary number and return them as an array.
Example 1:
输入: 2
输出: [0,1,1]
Example 2:
输入: 5
输出: [0,1,1,2,1,2]
Advanced:
- It is very easy to give a time complexity of O(n*sizeof(integer)) . But can you do it with one scan in linear time O(n)?
- The space complexity of the required algorithm is O(n) .
- Can you further refine the solution? It is required that no built-in functions (such as __builtin_popcount in C++) are used in C++ or any other language to perform this operation.
Two, solve
1. Violence
Ideas:
Perform bit calculation on any number x (0<=x<=num). Call the library function directly here, of course, you can also write by yourself.
Code:
class Solution {
public int[] countBits(int num) {
int[] cnt = new int[num+1];
for (int i=0; i<=num; i++) {
cnt[i] = Integer.bitCount(i);
}
return cnt;
}
}
Time complexity: O (n) O(n)O ( n ) , bitCount() can be obtained by 5 operations, the source code can be seen:191. The number of bits 1
space complexity: O (n) O(n)O ( n )
2. Overall shift
Ideas:
For any number i, its binary representation is the number cnt of 1, and there is such a relationship: cnt(i) = cnt(i/2) + i%2.
Under simple understanding:
If x = = 2 y + 0 x == 2y+0x==2 y+0,则 c n t ( x ) = = c n t ( y ) cnt(x)==cnt(y) cnt(x)==c n t ( y );
若x = = 2 y + zx == 2y + zx==2 y+z,则 c n t ( x ) = = c n t ( y ) + c n t ( z ) cnt(x)==cnt(y)+cnt(z) cnt(x)==c n t ( y )+c n t ( z ) .
Where z = x% 2 z=x\%2with=x%2, z z Why is z equal to this value? Now put y in binary, thenxxx为yyy is shifted to the left as a whole, and then at the end iszzz , two possibilities, 0 or 1, usexx%2x means that. Then list the numbers, binary representations and the number of 1s below for verification and understanding.
x 二进制 1个数
0 0 0
1 1 1
2 10 1
3 11 2
4 100 1
5 101 2
6 110 2
7 111 3
8 1000 1
9 1001 2
10 1010 2
11 1011 3
12 1100 2
13 1101 3
14 1110 3
15 1111 4
16 10000 1
Code: Version 1.
class Solution {
public int[] countBits(int num) {
int[] cnt= new int[num+1];
for (int i=0; i<=num; i++) {
cnt[i] = cnt[i/2] + i%2;
}
return cnt;
}
}
Code: Version 2, replaced by bit operation, which may be faster in operation.
class Solution {
public int[] countBits(int num) {
int[] cnt= new int[num+1];
for (int i=0; i<=num; i++) {
cnt[i] = cnt[i>>1] + (i&1);
}
return cnt;
}
}
Time complexity: O (n) O(n)O ( n )
space complexity: O (n) O(n)O ( n )
3. Bit operation
Ideas:
因为: i & ( i − 1 ) i\&(i-1) i&(i−1 ) Role: CleariiThe last 1 of i .
So:iii 1的个数== i & ( i − 1 ) i\&(i-1) i&(i−1 ) Number of 1 + 1
Code:
class Solution {
public int[] countBits(int num) {
int[] cnt= new int[num+1];
for (int i=1; i<=num; i++) {
cnt[i] = cnt[i&(i-1)] + 1;
}
return cnt;
}
}
Time complexity: O (n) O(n)O ( n )
space complexity: O (n) O(n)O ( n )
4. Dynamic planning
Ideas:
analyse as below:
Index : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
num of 1: 0 1 1 2 1 2 2 3 1 2 2 3 2 3 3 4
由上得出:
dp[0] = 0;
dp[1] = dp[0] + 1;
dp[2] = dp[0] + 1;
dp[3] = dp[1] +1;
dp[4] = dp[0] + 1;
dp[5] = dp[1] + 1;
dp[6] = dp[2] + 1;
dp[7] = dp[3] + 1;
dp[8] = dp[0] + 1;
dp[1] = dp[1-1] + 1;
dp[2] = dp[2-2] + 1;
dp[3] = dp[3-2] +1;
dp[4] = dp[4-4] + 1;
dp[5] = dp[5-4] + 1;
dp[6] = dp[6-4] + 1;
dp[7] = dp[7-4] + 1;
dp[8] = dp[8-8] + 1;
dp[index] = dp[index - offset] + 1;
Hey, the law is actually difficult to summarize, because the independent variable offset inside is also changing. Let's learn about it to provide a clue later.
Code:
public int[] countBits(int num) {
int result[] = new int[num + 1];
int offset = 1;
for (int index = 1; index < num + 1; ++index){
if (offset * 2 == index){
offset *= 2;
}
result[index] = result[index - offset] + 1;
}
return result;
}
Time complexity: O (n) O(n)O ( n )
space complexity: O (n) O(n)O ( n )
Three, reference
1. Three-Line Java Solution
2. How we handle this question on interview [Thinking process + DP solution]
3. Easy Understanding DP & Bit Java Solution
4, Java recursion O(n) time O(1) extra space 4ms
5. Bit counting
6, basic knowledge of bit operations