Skip to content

Commit dad857b

Browse files
committed
a new solution of 0
338
1 parent 09cbb32 commit dad857b

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

python/0338-counting-bits.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,17 @@ def countBits(self, n: int) -> List[int]:
88
offset = i
99
dp[i] = 1 + dp[i - offset]
1010
return dp
11+
12+
# Another dp solution
13+
class Solution2:
14+
def countBits(self, n: int) -> List[int]:
15+
res = [0] * (n + 1)
16+
for i in range(1, n + 1):
17+
if i % 2 == 1:
18+
res[i] = res[i - 1] + 1
19+
else:
20+
res[i] = res[i // 2]
21+
return res
22+
# This solution is based on the division of odd and even numbers.
23+
# I think it's easier to understand.
24+
# This is my full solution, covering the details: https://leetcode.com/problems/counting-bits/solutions/4411054/odd-and-even-numbers-a-easier-to-understanding-way-of-dp/

0 commit comments

Comments
 (0)