UNFINISH: 338

This commit is contained in:
sangge-redmi 2024-05-27 13:21:16 +08:00
parent dd88dae1bc
commit c1a78cca82

14
leetcode/338.py Normal file
View File

@ -0,0 +1,14 @@
# 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))