From b2812eb707d9e2c5382781b23d5add54e8e1db63 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 29 May 2023 19:02:51 +0800 Subject: [PATCH 1/3] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A2.md=20about=20rust?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...66\351\222\261\345\205\221\346\215\242.md" | 24 ++++++++++++------- 1 file changed, 15 insertions(+), 9 deletions(-) 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 0e3947dad3..28188349ff 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" @@ -315,18 +315,24 @@ 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] } } ``` From f45d4718c39c6d932ef32ff190c01be839a7d18d Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 29 May 2023 19:10:46 +0800 Subject: [PATCH 2/3] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...66\351\222\261\345\205\221\346\215\242.md" | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) 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 28188349ff..0f910451ed 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" @@ -336,6 +336,28 @@ impl Solution { } ``` +```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 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) => { From 88f03c4abaf66141a2e507c746633af2cd93c7e6 Mon Sep 17 00:00:00 2001 From: fwqaaq Date: Mon, 29 May 2023 23:26:49 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Update=200322.=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A2.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...66\351\222\261\345\205\221\346\215\242.md" | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) 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 0f910451ed..7272b194c5 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" @@ -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) @@ -360,6 +360,7 @@ impl Solution { Javascript: ```javascript +// 遍历物品 const coinChange = (coins, amount) => { if(!amount) { return 0; @@ -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]); } @@ -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; @@ -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] +} +``` + +