1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <vector> using std::vector; class Solution { public: vector<int> countBits(int num) { vector<int> result(num + 1); result[0] = 0; for (int i = 1; i <= num; i++) { if (i % 2 == 1) { result[i] = result[i - 1] + 1; } else { result[i] = result[i / 2]; } } return result; } };