mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Update 0322.零钱兑换.md
This commit is contained in:
@ -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) => {
|
||||
|
Reference in New Issue
Block a user