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 b2812eb commit f45d471
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions problems/0322.零钱兑换.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,28 @@ impl Solution {
}
```

```rust
// 遍历背包
impl Solution {
pub fn coin_change(coins: Vec<i32>, amount: i32) -> i32 {
let amount = amount as usize;
let mut dp = vec![i32::MAX; amount + 1];
dp[0] = 0;
for i in 0..=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)
}
}
}
if dp[amount] == i32::MAX {
return -1;
}
dp[amount]
}
}
```

Javascript:
```javascript
const coinChange = (coins, amount) => {
Expand Down

0 comments on commit f45d471

Please sign in to comment.