Skip to content

Counting Bits

EasyBit Manipulation & Math
Decode it first. Clues, constraints, pattern and plan before any code.

Return a list ans of length n + 1 where ans[i] is the number of 1 bits in the binary form of i, for every i from 0 to n. Aim for a single O(n) pass.

n = 2  →  [0, 1, 1]            (0, 1, 10)
n = 5  →  [0, 1, 1, 2, 1, 2]   (0, 1, 10, 11, 100, 101)

Constraints

  • 0 <= n <= 10^5