LeetCode416. 分割等和子集
- 题目链接
- 代码
题目链接
https://leetcode.cn/problems/partition-equal-subset-sum/
代码
class Solution:def canPartition(self, nums: List[int]) -> bool:sum = 0dp = [0]*10001for num in nums:sum += numif sum % 2 == 1:return Falsetarget = sum //2for num in nums:for i in range(target, num - 1, -1):dp[i] = max(dp[i], dp[i - num] + num)if dp[target] == target:return Truereturn False