From 4dda01ba256a0584fbe20a88ceb73335eb27269c Mon Sep 17 00:00:00 2001 From: haofeng <852172305@qq.com> Date: Sun, 6 Jun 2021 11:44:12 +0800 Subject: [PATCH] =?UTF-8?q?Update=200474.=E4=B8=80=E5=92=8C=E9=9B=B6.md=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=80=E5=92=8C=E9=9B=B6=20python3=20?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0474.一和零.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/problems/0474.一和零.md b/problems/0474.一和零.md index e158ca63..1e0dc794 100644 --- a/problems/0474.一和零.md +++ b/problems/0474.一和零.md @@ -191,6 +191,31 @@ class Solution { Python: +```python3 +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: + dp = [[0]*(n + 1) for _ in range(m + 1)] # 默认初始化0 + # 遍历物品 + for i in range(len(strs)): + # num_zeros = strs[i].count('0') + # num_ones = strs[i].count('1') + num_zeros, num_ones = self.countBinary(strs[i]) + # 遍历背包容量且从后向前遍历! + for j in range(m, num_zeros - 1, -1): + for i in range(n, num_ones - 1, -1): + dp[j][i] = max(dp[j - num_zeros][i - num_ones] + 1, dp[j][i]) + return dp[m][n] +``` + Go: