diff --git a/338-Counting-Bits.py b/338-Counting-Bits.py new file mode 100644 index 000000000..81e7f3655 --- /dev/null +++ b/338-Counting-Bits.py @@ -0,0 +1,10 @@ +class Solution: + def countBits(self, n: int) -> List[int]: + dp = [0] * (n + 1) + offset = 1 + + for i in range(1, n + 1): + if offset * 2 == i: + offset = i + dp[i] = 1 + dp[i - offset] + return dp