mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Update 0377.组合总和Ⅳ.md
This commit is contained in:
@ -175,22 +175,36 @@ class Solution {
|
||||
```
|
||||
|
||||
Python:
|
||||
卡哥版
|
||||
```python
|
||||
class Solution:
|
||||
def combinationSum4(self, nums: List[int], target: int) -> int:
|
||||
dp = [0] * (target + 1)
|
||||
dp[0] = 1
|
||||
for i in range(1, target + 1): # 遍历背包
|
||||
for j in range(len(nums)): # 遍历物品
|
||||
if i - nums[j] >= 0:
|
||||
dp[i] += dp[i - nums[j]]
|
||||
return dp[target]
|
||||
|
||||
```
|
||||
优化版
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def combinationSum4(self, nums, target):
|
||||
dp = [0] * (target + 1)
|
||||
dp[0] = 1
|
||||
def combinationSum4(self, nums: List[int], target: int) -> int:
|
||||
dp = [0] * (target + 1) # 创建动态规划数组,用于存储组合总数
|
||||
dp[0] = 1 # 初始化背包容量为0时的组合总数为1
|
||||
|
||||
for i in range(1, target + 1): # 遍历背包容量
|
||||
for j in nums: # 遍历物品列表
|
||||
if i >= j: # 当背包容量大于等于当前物品重量时
|
||||
dp[i] += dp[i - j] # 更新组合总数
|
||||
|
||||
return dp[-1] # 返回背包容量为target时的组合总数
|
||||
|
||||
for i in range(1, target+1):
|
||||
for j in nums:
|
||||
if i >= j:
|
||||
dp[i] += dp[i - j]
|
||||
|
||||
return dp[-1]
|
||||
```
|
||||
|
||||
|
||||
Go:
|
||||
```go
|
||||
func combinationSum4(nums []int, target int) int {
|
||||
|
Reference in New Issue
Block a user