UNFINISH: 338
This commit is contained in:
parent
dd88dae1bc
commit
c1a78cca82
14
leetcode/338.py
Normal file
14
leetcode/338.py
Normal 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))
|
Loading…
x
Reference in New Issue
Block a user