Counting Bits
For every integer i from zero through n, return its number of one bits.
Example 1
n = 5[0,1,1,2,1,2]Explanation
The binary values 0..5 have counts 0,1,1,2,1,2.
Example 2
n = 0[0]Explanation
Include zero itself, so n=0 gives a one-element result.
Example 3
n = 8[0,1,1,2,1,2,2,3,1]Explanation
8 is binary 1000 with one set bit; retain all preceding counts too.
Constraints
- 0 <= n <= 100000
Function contract
countBitsArguments: jsonSolutionRuns on your device
Loading draft…
Run: 3 examples; Submit: 33 cases, on your device.
5