问题描述
可以用动态规划做,然后运行速度超级慢。。。
这边的问题的j的取值
class Solution:
def canPartition(self, nums: List[int]) -> bool:
target = nums[0]
n = len(nums)
for i in range(1,n):
target += nums[i]
if target % 2 == 1:
return False
target = target//2
dp = [0]*(target+1)
for i in range(n):
for j in range(target,nums[i]-1,-1):
dp[j] = max(dp[j],dp[j-nums[i]]+nums[i])
return dp[target] == target
运行结果: