Skip to content

Commit 47333f4

Browse files
committed
feat: add cpp solution to lc problem: No.0518. Coin Change 2
1 parent 9f0e89f commit 47333f4

File tree

5 files changed

+57
-2
lines changed

5 files changed

+57
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@
199199
- [最长公共子序列](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README.md)
200200
- [编辑距离](./solution/0000-0099/0072.Edit%20Distance/README.md)
201201
- [零钱兑换](./solution/0300-0399/0322.Coin%20Change/README.md)
202+
- [零钱兑换 II](./solution/0500-0599/0518.Coin%20Change%202/README.md)
202203
- [礼物的最大价值](./lcof/面试题47.%20礼物的最大价值/README.md)
203204
- [俄罗斯套娃信封问题](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README.md)
204205

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
193193
- [Longest Common Subsequence](./solution/1100-1199/1143.Longest%20Common%20Subsequence/README_EN.md)
194194
- [Edit Distance](./solution/0000-0099/0072.Edit%20Distance/README_EN.md)
195195
- [Coin Change](./solution/0300-0399/0322.Coin%20Change/README_EN.md)
196+
- [Coin Change 2](./solution/0500-0599/0518.Coin%20Change%202/README_EN.md)
196197
- [Russian Doll Envelopes](./solution/0300-0399/0354.Russian%20Doll%20Envelopes/README_EN.md)
197198

198199
### Backtracking

solution/0500-0599/0518.Coin Change 2/README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555

5656
<!-- 这里可写通用的实现逻辑 -->
5757

58-
完全背包问题
58+
动态规划。
59+
60+
完全背包问题。
5961

6062
<!-- tabs:start -->
6163

@@ -123,6 +125,24 @@ func change(amount int, coins []int) int {
123125
}
124126
```
125127

128+
### **C++**
129+
130+
```cpp
131+
class Solution {
132+
public:
133+
int change(int amount, vector<int>& coins) {
134+
vector<int> dp(amount + 1);
135+
dp[0] = 1;
136+
for (auto coin : coins) {
137+
for (int j = coin; j <= amount; ++j) {
138+
dp[j] += dp[j - coin];
139+
}
140+
}
141+
return dp[amount];
142+
}
143+
};
144+
```
145+
126146
### **...**
127147
128148
```

solution/0500-0599/0518.Coin Change 2/README_EN.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@
9595

9696
## Solutions
9797

98-
Complete knapsack problem
98+
Dynamic programming.
99+
100+
Complete knapsack problem.
99101

100102
<!-- tabs:start -->
101103

@@ -159,6 +161,24 @@ func change(amount int, coins []int) int {
159161
}
160162
```
161163

164+
### **C++**
165+
166+
```cpp
167+
class Solution {
168+
public:
169+
int change(int amount, vector<int>& coins) {
170+
vector<int> dp(amount + 1);
171+
dp[0] = 1;
172+
for (auto coin : coins) {
173+
for (int j = coin; j <= amount; ++j) {
174+
dp[j] += dp[j - coin];
175+
}
176+
}
177+
return dp[amount];
178+
}
179+
};
180+
```
181+
162182
### **...**
163183
164184
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
public:
3+
int change(int amount, vector<int>& coins) {
4+
vector<int> dp(amount + 1);
5+
dp[0] = 1;
6+
for (auto coin : coins) {
7+
for (int j = coin; j <= amount; ++j) {
8+
dp[j] += dp[j - coin];
9+
}
10+
}
11+
return dp[amount];
12+
}
13+
};

0 commit comments

Comments
 (0)