diff --git a/leetcode/338.py b/leetcode/338.py new file mode 100644 index 0000000..99a2d72 --- /dev/null +++ b/leetcode/338.py @@ -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))