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

Commit 4dda01b

Browse files
Update 0474.一和零.md
添加一和零 python3 版本
1 parent c591dc1 commit 4dda01b

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

‎problems/0474.一和零.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,31 @@ class Solution {
191191

192192
Python:
193193

194+
```python3
195+
class Solution:
196+
def countBinary(self, s: str) -> (int, int):
197+
zeros, ones = 0, 0
198+
for c in s:
199+
if c == '0':
200+
zeros += 1
201+
else:
202+
ones += 1
203+
return zeros, ones
204+
205+
def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
206+
dp = [[0]*(n + 1) for _ in range(m + 1)] # 默认初始化0
207+
# 遍历物品
208+
for i in range(len(strs)):
209+
# num_zeros = strs[i].count('0')
210+
# num_ones = strs[i].count('1')
211+
num_zeros, num_ones = self.countBinary(strs[i])
212+
# 遍历背包容量且从后向前遍历!
213+
for j in range(m, num_zeros - 1, -1):
214+
for i in range(n, num_ones - 1, -1):
215+
dp[j][i] = max(dp[j - num_zeros][i - num_ones] + 1, dp[j][i])
216+
return dp[m][n]
217+
```
218+
194219

195220
Go:
196221

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /