题解 | #目标和#
目标和
https://www.nowcoder.com/practice/7fc06e2162f048658252fac542fcb1e8
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param target int整型 # @return int整型 # class Solution: def findTargetSumWays(self , nums: List[int], target: int) -> int: # write code here total = sum(nums) return 0 if total <target or (total&1)^(target&1)!=0 or len(nums)==0 else self.subset(nums,(total+target)>>1) def subset(self,nums,target): if target<0: return 0 dp = [0] * (target+1) dp[0] = 1 for n in nums: for j in range(target,n-1,-1): dp[j] += dp[j-n] return dp[target]