mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge pull request #301 from LiangDazhu/patch-26
添加 0494.目标和.md python版本
This commit is contained in:
@ -150,7 +150,7 @@ dp[j] 表示:填满j(包括j)这么大容积的包,有dp[i]种方法
|
||||
|
||||
有哪些来源可以推出dp[j]呢?
|
||||
|
||||
不考虑nums[i]的情况下,填满容量为j - nums[i]的背包,有dp[j - nums[i]]中方法。
|
||||
不考虑nums[i]的情况下,填满容量为j - nums[i]的背包,有dp[j - nums[i]]种方法。
|
||||
|
||||
那么只要搞到nums[i]的话,凑成dp[j]就有dp[j - nums[i]] 种方法。
|
||||
|
||||
@ -261,7 +261,19 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def findTargetSumWays(self, nums: List[int], target: int) -> int:
|
||||
sumValue = sum(nums)
|
||||
if target > sumValue or (sumValue + target) % 2 == 1: return 0
|
||||
bagSize = (sumValue + target) // 2
|
||||
dp = [0] * (bagSize + 1)
|
||||
dp[0] = 1
|
||||
for i in range(len(nums)):
|
||||
for j in range(bagSize, nums[i] - 1, -1):
|
||||
dp[j] += dp[j - nums[i]]
|
||||
return dp[bagSize]
|
||||
```
|
||||
|
||||
Go:
|
||||
|
||||
|
Reference in New Issue
Block a user