Update 0474.一和零.md

添加 python3 注释
This commit is contained in:
haofeng
2021-06-06 13:03:27 +08:00

View File

@ -190,33 +190,21 @@ class Solution {
``` ```
Python Python
```python3 ```python3
class Solution: class Solution:
def countBinary(self, s: str) -> (int, int):
zeros, ones = 0, 0
for c in s:
if c == '0':
zeros += 1
else:
ones += 1
return zeros, ones
def findMaxForm(self, strs: List[str], m: int, n: int) -> int: def findMaxForm(self, strs: List[str], m: int, n: int) -> int:
dp = [[0] * (n + 1) for _ in range(m + 1)] # 默认初始化0 dp = [[0] * (n + 1) for _ in range(m + 1)] # 默认初始化0
# 遍历物品 # 遍历物品
for i in range(len(strs)): for str in strs:
# num_zeros = strs[i].count('0') ones = str.count('1')
# num_ones = strs[i].count('1') zeros = str.count('0')
num_zeros, num_ones = self.countBinary(strs[i])
# 遍历背包容量且从后向前遍历! # 遍历背包容量且从后向前遍历!
for j in range(m, num_zeros - 1, -1): for i in range(m, zeros - 1, -1):
for i in range(n, num_ones - 1, -1): for j in range(n, ones - 1, -1):
dp[j][i] = max(dp[j - num_zeros][i - num_ones] + 1, dp[j][i]) dp[i][j] = max(dp[i][j], dp[i - zeros][j - ones] + 1)
return dp[m][n] return dp[m][n]
``` ```
Go Go