15 lines
425 B
Python

# 338. Counting Bits
# 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.
class Solution:
def countBits(self, n: int) -> list[int]:
ans = []
for i in range(n + 1):
ans.append(bin(i).count("1"))
return ans
if __name__ == "__main__":
n = 5
print(Solution().countBits(n))