website/content/ChapterFour/0300~0399/0338.Counting-Bits.md
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:
给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。
给出一个数,要求计算出 0 ≤ i ≤ num 中每个数的二进制位 1 的个数。
这一题就是利用二进制位运算的经典题。
X&1==1or==0,可以用 X&1 判断奇偶性,X&1>0 即奇数。
X = X & (X-1) 清零最低位的1
X & -X => 得到最低位的1
X&~X=>0
package leetcode
func countBits(num int) []int {
bits := make([]int, num+1)
for i := 1; i <= num; i++ {
bits[i] += bits[i&(i-1)] + 1
}
return bits
}