File tree 5 files changed +97
-9
lines changed
solution/0500-0599/0518.Coin Change 2
5 files changed +97
-9
lines changed Original file line number Diff line number Diff line change 33
33
34
34
<p ><strong >示例 3:</strong ></p >
35
35
36
- <pre ><strong >输入:</strong > amount = 10, coins = [10]
36
+ <pre ><strong >输入:</strong > amount = 10, coins = [10]
37
37
<strong >输出:</strong > 1
38
38
</pre >
39
39
55
55
56
56
<!-- 这里可写通用的实现逻辑 -->
57
57
58
+ 完全背包问题
59
+
58
60
<!-- tabs:start -->
59
61
60
62
### ** Python3**
61
63
62
64
<!-- 这里可写当前语言的特殊实现逻辑 -->
63
65
64
66
``` python
65
-
67
+ class Solution :
68
+ def change (self , amount : int , coins : List[int ]) -> int :
69
+ dp = [0 for i in range (amount + 1 )]
70
+ dp[0 ] = 1
71
+ for coin in coins:
72
+ for j in range (coin, amount + 1 ):
73
+ dp[j] += dp[j - coin]
74
+ return dp[amount]
66
75
```
67
76
68
77
### ** Java**
69
78
70
79
<!-- 这里可写当前语言的特殊实现逻辑 -->
71
80
72
81
``` java
82
+ class Solution {
83
+ public int change (int amount , int [] coins ) {
84
+ int [] dp = new int [amount + 1 ];
85
+ dp[0 ] = 1 ;
86
+ for (int coin : coins) {
87
+ for (int j = coin; j <= amount; j++ ) {
88
+ dp[j] += dp[j - coin];
89
+ }
90
+ }
91
+ return dp[amount];
92
+ }
93
+ }
94
+ ```
73
95
96
+ ### ** Go**
97
+
98
+ ``` go
99
+ func change (amount int , coins []int ) int {
100
+ dp := make ([]int , amount+1 )
101
+ dp[0 ] = 1
102
+ for _ , coin := range coins {
103
+ for j := coin; j <= amount; j++ {
104
+ dp[j] += dp[j-coin]
105
+ }
106
+ }
107
+ return dp[amount]
108
+ }
74
109
```
75
110
76
111
### ** ...**
Original file line number Diff line number Diff line change 64
64
65
65
<pre >
66
66
67
- <b >Input:</b > amount = 10, coins = [10]
67
+ <b >Input:</b > amount = 10, coins = [10]
68
68
69
69
<b >Output:</b > 1
70
70
95
95
96
96
## Solutions
97
97
98
+ Complete knapsack problem
99
+
98
100
<!-- tabs:start -->
99
101
100
102
### ** Python3**
101
103
102
104
``` python
103
-
105
+ class Solution :
106
+ def change (self , amount : int , coins : List[int ]) -> int :
107
+ dp = [0 for i in range (amount + 1 )]
108
+ dp[0 ] = 1
109
+ for coin in coins:
110
+ for j in range (coin, amount + 1 ):
111
+ dp[j] += dp[j - coin]
112
+ return dp[amount]
104
113
```
105
114
106
115
### ** Java**
107
116
108
117
``` java
118
+ class Solution {
119
+ public int change (int amount , int [] coins ) {
120
+ int [] dp = new int [amount + 1 ];
121
+ dp[0 ] = 1 ;
122
+ for (int coin : coins) {
123
+ for (int j = coin; j <= amount; j++ ) {
124
+ dp[j] += dp[j - coin];
125
+ }
126
+ }
127
+ return dp[amount];
128
+ }
129
+ }
130
+ ```
109
131
132
+ ### ** Go**
133
+
134
+ ``` go
135
+ func change (amount int , coins []int ) int {
136
+ dp := make ([]int , amount+1 )
137
+ dp[0 ] = 1
138
+ for _ , coin := range coins {
139
+ for j := coin; j <= amount; j++ {
140
+ dp[j] += dp[j-coin]
141
+ }
142
+ }
143
+ return dp[amount]
144
+ }
110
145
```
111
146
112
147
### ** ...**
Original file line number Diff line number Diff line change
1
+ func change (amount int , coins []int ) int {
2
+ dp := make ([]int , amount + 1 )
3
+ dp [0 ] = 1
4
+ for _ , coin := range coins {
5
+ for j := coin ; j <= amount ; j ++ {
6
+ dp [j ] += dp [j - coin ]
7
+ }
8
+ }
9
+ return dp [amount ]
10
+ }
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public int change (int amount , int [] coins ) {
3
- int [] f = new int [amount + 1 ];
4
- f [0 ] = 1 ;
3
+ int [] dp = new int [amount + 1 ];
4
+ dp [0 ] = 1 ;
5
5
for (int coin : coins ) {
6
- for (int i = coin ; i <= amount ; ++ i ) {
7
- f [ i ] += f [ i - coin ];
6
+ for (int j = coin ; j <= amount ; j ++ ) {
7
+ dp [ j ] += dp [ j - coin ];
8
8
}
9
9
}
10
- return f [amount ];
10
+ return dp [amount ];
11
11
}
12
12
}
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def change (self , amount : int , coins : List [int ]) -> int :
3
+ dp = [0 for i in range (amount + 1 )]
4
+ dp [0 ] = 1
5
+ for coin in coins :
6
+ for j in range (coin , amount + 1 ):
7
+ dp [j ] += dp [j - coin ]
8
+ return dp [amount ]
You can’t perform that action at this time.
0 commit comments