My Vault

Home

❯

leetcode

❯

count bits

count-bits

Jul 04, 20241 min read

  • cse/algorithm
  • leetcode

Counting Bits

https://leetcode.com/problems/counting-bits/

This solution beats 95.54% in time.

ans = [0] * (n + 1)
for i in range(1, n + 1):
    ans[i] = ans[i >> 1] + (i & 1)
return ans

Graph View

Created with Quartz v4.5.2 © 2026

  • GitHub
  • Homepage