Skip to content

Commit

Permalink
Update 0322.零钱兑换.md
Browse files Browse the repository at this point in the history
  • Loading branch information
fwqaaq authored May 29, 2023
1 parent f45d471 commit 88f03c4
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions problems/0322.零钱兑换.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ impl Solution {
let amount = amount as usize;
let mut dp = vec![i32::MAX; amount + 1];
dp[0] = 0;
for i in 0..=amount {
for i in 1..=amount {
for &coin in &coins {
if i >= coin as usize && dp[i - coin as usize] != i32::MAX {
dp[i] = dp[i].min(dp[i - coin as usize] + 1)
Expand All @@ -360,6 +360,7 @@ impl Solution {

Javascript:
```javascript
// 遍历物品
const coinChange = (coins, amount) => {
if(!amount) {
return 0;
Expand All @@ -368,7 +369,7 @@ const coinChange = (coins, amount) => {
let dp = Array(amount + 1).fill(Infinity);
dp[0] = 0;

for(let i =0; i < coins.length; i++) {
for(let i = 0; i < coins.length; i++) {
for(let j = coins[i]; j <= amount; j++) {
dp[j] = Math.min(dp[j - coins[i]] + 1, dp[j]);
}
Expand All @@ -378,9 +379,26 @@ const coinChange = (coins, amount) => {
}
```

```javascript
// 遍历背包
var coinChange = function(coins, amount) {
const dp = Array(amount + 1).fill(Infinity)
dp[0] = 0
for (let i = 1; i <= amount; i++) {
for (let j = 0; j < coins.length; j++) {
if (i >= coins[j] && dp[i - coins[j]] !== Infinity) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1)
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount]
}
```

TypeScript:

```typescript
// 遍历物品
function coinChange(coins: number[], amount: number): number {
const dp: number[] = new Array(amount + 1).fill(Infinity);
dp[0] = 0;
Expand All @@ -394,6 +412,23 @@ function coinChange(coins: number[], amount: number): number {
};
```

```typescript
// 遍历背包
function coinChange(coins: number[], amount: number): number {
const dp: number[] = Array(amount + 1).fill(Infinity)
dp[0] = 0
for (let i = 1; i <= amount; i++) {
for (let j = 0; j < coins.length; j++) {
if (i >= coins[j] && dp[i - coins[j]] !== Infinity) {
dp[i] = Math.min(dp[i], dp[i - coins[j]] + 1)
}
}
}
return dp[amount] === Infinity ? -1 : dp[amount]
}
```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
Expand Down

0 comments on commit 88f03c4

Please sign in to comment.