0338.cpp (286B)
1 class Solution { 2 public: 3 vector<int> countBits(int n) { 4 vector<int> res(n + 1); 5 int offset = 1; 6 for (int i = 1; i <= n; i++) { 7 if (offset * 2 == i) offset *= 2; 8 res[i] = (res[i - offset]) + 1; 9 } 10 return res; 11 } 12 };