Loading problem…
Given an integer n, return an array ans of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.
Example 1
Input: n = 2
Output: [0,1,1]
Explanation: 0 is 0 in binary (no 1s), 1 is 1 (one 1), and 2 is 10 (one 1), so the answer is [0,1,1].
Example 2
Input: n = 5
Output: [0,1,1,2,1,2]
Explanation: In binary, 0 = 0, 1 = 1, 2 = 10, 3 = 11, 4 = 100 and 5 = 101. Counting the 1s gives [0,1,1,2,1,2].
Example 3
Input: n = 0
Output: [0]
Explanation: Only the number 0 is included, and it has no 1 bits.
Dropping the last bit of i gives i // 2, whose bit count we already computed. Add back the bit we dropped (i & 1).
Run checks all cases above. Submit evaluates all test cases.