File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -268,6 +268,7 @@ class Solution:
268268
269269### Go:
270270
271+ 一维dp
271272``` go
272273func change (amount int , coins []int ) int {
273274 // 定义dp数组
@@ -286,6 +287,29 @@ func change(amount int, coins []int) int {
286287 return dp[amount]
287288}
288289```
290+ 二维dp
291+ ``` go
292+ func change (amount int , coins []int ) int {
293+ dp := make ([][]int , len (coins))
294+ for i := range dp {
295+ dp[i] = make ([]int , amount + 1 )
296+ dp[i][0 ] = 1
297+ }
298+ for j := coins[0 ]; j <= amount; j++ {
299+ dp[0 ][j] += dp[0 ][j-coins[0 ]]
300+ }
301+ for i := 1 ; i < len (coins); i++ {
302+ for j := 1 ; j <= amount; j++ {
303+ if j < coins[i] {
304+ dp[i][j] = dp[i-1 ][j]
305+ } else {
306+ dp[i][j] = dp[i][j-coins[i]] + dp[i-1 ][j]
307+ }
308+ }
309+ }
310+ return dp[len (coins)-1 ][amount]
311+ }
312+ ```
289313
290314### Rust:
291315
You can’t perform that action at this time.
0 commit comments