classSolution(object): deffindTargetSumWays(self, nums, S): """ :type nums: List[int] :type S: int :rtype: int """ # p - n = S # p - n + n = S + n # p + n = S + 2n # sum(nums) = S + 2n # n = (sum(nums) - S)/2 N = sum(nums) if S > N or (N-S)%2: return0 target = (N - S)/2 dp = [1] + [0] * target for i in nums: for j inrange(target, i-1, -1): dp[j] += dp[j-i] return dp[-1]