diff --git "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" index 67b1a6d9cd..1f3f4df27f 100644 --- "a/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" +++ "b/problems/0322.\351\233\266\351\222\261\345\205\221\346\215\242.md" @@ -355,23 +355,52 @@ func min(a, b int) int { Rust: ```rust -pub fn coin_change(coins: Vec, amount: i32) -> i32 { - let amount = amount as usize; - let mut dp = vec![i32::MAX; amount + 1]; - dp[0] = 0; - for i in 0..coins.len() { - for j in coins[i] as usize..=amount { - if dp[j - coins[i] as usize] != i32::MAX { - dp[j] = dp[j].min(dp[j - coins[i] as usize] + 1); +// 遍历物品 +impl Solution { + pub fn coin_change(coins: Vec, amount: i32) -> i32 { + let amount = amount as usize; + let mut dp = vec![i32::MAX; amount + 1]; + dp[0] = 0; + for coin in coins { + for i in coin as usize..=amount { + if 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] } - if dp[amount] == i32::MAX { -1 } else { dp[amount] } +} +``` + +```rust +// 遍历背包 +impl Solution { + pub fn coin_change(coins: Vec, amount: i32) -> i32 { + let amount = amount as usize; + let mut dp = vec![i32::MAX; amount + 1]; + dp[0] = 0; + 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) + } + } + } + if dp[amount] == i32::MAX { + return -1; + } + dp[amount] + } } ``` Javascript: ```javascript +// 遍历物品 const coinChange = (coins, amount) => { if(!amount) { return 0; @@ -380,7 +409,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]); } @@ -390,9 +419,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; @@ -406,6 +452,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] +} +``` + +