mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Merge pull request #2730 from markwang1992/518-change
518.零钱兑换II增加Go二维dp解法
This commit is contained in:
@ -268,6 +268,7 @@ class Solution:
|
|||||||
|
|
||||||
### Go:
|
### Go:
|
||||||
|
|
||||||
|
一维dp
|
||||||
```go
|
```go
|
||||||
func change(amount int, coins []int) int {
|
func change(amount int, coins []int) int {
|
||||||
// 定义dp数组
|
// 定义dp数组
|
||||||
@ -286,6 +287,29 @@ func change(amount int, coins []int) int {
|
|||||||
return dp[amount]
|
return dp[amount]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
二维dp
|
||||||
|
```go
|
||||||
|
func change(amount int, coins []int) int {
|
||||||
|
dp := make([][]int, len(coins))
|
||||||
|
for i := range dp {
|
||||||
|
dp[i] = make([]int, amount + 1)
|
||||||
|
dp[i][0] = 1
|
||||||
|
}
|
||||||
|
for j := coins[0]; j <= amount; j++ {
|
||||||
|
dp[0][j] += dp[0][j-coins[0]]
|
||||||
|
}
|
||||||
|
for i := 1; i < len(coins); i++ {
|
||||||
|
for j := 1; j <= amount; j++ {
|
||||||
|
if j < coins[i] {
|
||||||
|
dp[i][j] = dp[i-1][j]
|
||||||
|
} else {
|
||||||
|
dp[i][j] = dp[i][j-coins[i]] + dp[i-1][j]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dp[len(coins)-1][amount]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### Rust:
|
### Rust:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user