leetcode 338. Counting Bits #Medium
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].
所求序列包含着规律:设数字n的二进制表示包含1的个数是y,数字n/2的二进制表示包含1的个数是x;当n为奇数时y=x+1,n为偶数时y=x。
class Solution {
public:
vector
countBits(int num) {
vector
r(num+1,0);
for(int i=1;i