#338
Counting Bits
easy· Bit Manipulationruns: 0Given an integer n, return an array of length n + 1 such that ans[i] is the number of 1 bits in the binary representation of i, for each i from 0 to n.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 197
class Solution:
def countBits(self, n: int) -> list[int]:
result = [0] * (n + 1)
for i in range(1, n + 1):
result[i] = result[i & (i - 1)] + 1
return result
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.