python-leetcode-分割等和子集

416. 分割等和子集 - 力扣(LeetCode)

python-leetcode-分割等和子集_第1张图片

class Solution:
    def canPartition(self, nums: List[int]) -> bool:
        total = sum(nums)
        if total % 2 != 0:
            return False
        
        target = total // 2
        dp = [False] * (target + 1)
        dp[0] = True

        for num in nums:
            for j in range(target, num - 1, -1):
                dp[j] = dp[j] or dp[j - num]
        
        return dp[target]

你可能感兴趣的:(python,leetcode,算法,职场和发展)