File tree Expand file tree Collapse file tree 2 files changed +43
-0
lines changed
solution/0300-0399/0322.Coin Change Expand file tree Collapse file tree 2 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -68,4 +68,28 @@ var coinChange = function (coins, amount) {
6868};
6969```
7070
71+ ### ** C++**
72+
73+ ``` cpp
74+ class Solution {
75+ public:
76+ int coinChange(vector<int >& coins, int amount) {
77+ std::vector<int > dp(amount + 1, -1);
78+ dp[ 0] = 0;
79+ for (int i = 1; i <= amount; i++) {
80+ for (int j = 0; j < coins.size(); j++) {
81+ if (coins[ j] <= i && dp[ i - coins[ j]] != -1) {
82+ // 当 当前值未被计算,或者有更小的组成方式的情况下
83+ if (dp[ i] == -1 || dp[ i] > dp[ i - coins[ j]] + 1) {
84+ dp[ i] = dp[ i - coins[ j]] + 1;
85+ }
86+ }
87+ }
88+ }
89+
90+ return dp[amount];
91+ }
92+ };
93+ ```
94+
7195<!-- tabs:end -->
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int coinChange (vector<int >& coins, int amount) {
4+ std::vector<int > dp (amount + 1 , -1 );
5+ dp[0 ] = 0 ;
6+ for (int i = 1 ; i <= amount; i++) {
7+ for (int j = 0 ; j < coins.size (); j++) {
8+ if (coins[j] <= i && dp[i - coins[j]] != -1 ) {
9+ // 当 当前值未被计算,或者有更小的组成方式的情况下
10+ if (dp[i] == -1 || dp[i] > dp[i - coins[j]] + 1 ) {
11+ dp[i] = dp[i - coins[j]] + 1 ;
12+ }
13+ }
14+ }
15+ }
16+
17+ return dp[amount];
18+ }
19+ };
You can’t perform that action at this time.
0 commit comments