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:
For num = 5
you should return [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.
给定一个数N,得到0到N的所有的数的二进制数中包含的1,存入数组返回。
public class Solution {
public int[] countBits(int num) {
int[] list=new int[num+1];
for(int i=0;i<=num;i++){
int count=0;
int j=i;
while(j!=0){
count++;
j=j&(j-1);
}
list[i]=count;
}
return list;
}
}
public int[] countBits(int num) {
int[] list=new int[num+1];
for(int i=0;i<=num;i++){
int count=0;
int j=i;
while(j!=0){
count++;
j=j&(j-1);
}
list[i]=count;
}
return list;
}
}
这道题很快就写出来了,因为之前编程之美上看到过一个方法求一个整数的二进制中包含1的个数。就是采用n&n-1的方法
为什么用n&n-1就能很快得到二进制中包含1的个数呢? 下面进行分析:
(1)假设n为奇数100111
减去1后得到的数 100110
两数相与 得到 100110
(2)假设n为偶数101100
减去1后得到的数 101011
两数相与 得到 101000
从上面的说明得出,其实n&n-1每次做的就是从二进制中扣除最后面那个1.很神奇吧