Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdad857b

Browse files
committed
a new solution of 0
338
1 parent09cbb32 commitdad857b

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
returndp
11+
12+
# Another dp solution
13+
classSolution2:
14+
defcountBits(self,n:int)->List[int]:
15+
res= [0]* (n+1)
16+
foriinrange(1,n+1):
17+
ifi%2==1:
18+
res[i]=res[i-1]+1
19+
else:
20+
res[i]=res[i//2]
21+
returnres
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)

[8]ページ先頭

©2009-2025 Movatter.jp