From bf2ffec6674a429663a11aca03abbd9f36a9864c Mon Sep 17 00:00:00 2001 From: LiangDazhu <42199191+LiangDazhu@users.noreply.github.com> Date: Sun, 30 May 2021 19:21:51 +0800 Subject: [PATCH] =?UTF-8?q?Update=200416.=E5=88=86=E5=89=B2=E7=AD=89?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E9=9B=86.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Added python version code --- problems/0416.分割等和子集.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/problems/0416.分割等和子集.md b/problems/0416.分割等和子集.md index e69eb1a4..55d200e2 100644 --- a/problems/0416.分割等和子集.md +++ b/problems/0416.分割等和子集.md @@ -222,8 +222,18 @@ class Solution { ``` Python: - - +```python +class Solution: + def canPartition(self, nums: List[int]) -> bool: + taraget = sum(nums) + if taraget % 2 == 1: return False + taraget //= 2 + dp = [0] * 10001 + for i in range(len(nums)): + for j in range(taraget, nums[i] - 1, -1): + dp[j] = max(dp[j], dp[j - nums[i]] + nums[i]) + return taraget == dp[taraget] +``` Go: