Counting Bits

 

CREATED TIME: Jan 12, 2020 12:19 AM UPDATED TIME: Jan 12, 2020 12:19 AM 난이도: Medium

class Solution { public: vector countBits(int num) {

        vector<int> nums(num + 1, 0);
        
        for(int i=0; i<=num; i++){
            nums[i] = nums[i >> 1] + (i & 1);
        }
        return nums;
    }
};