From 7ac217942fea957c2d8cfb579e81d80bbd590499 Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Wed, 26 Jul 2023 15:28:27 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E8=83=8C=E5=8C=85=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=200139.?= =?UTF-8?q?=E5=8D=95=E8=AF=8D=E6=8B=86=E5=88=86=200279.=E5=AE=8C=E5=85=A8?= =?UTF-8?q?=E5=B9=B3=E6=96=B9=E6=95=B0=200322.=E9=9B=B6=E9=92=B1=E5=85=91?= =?UTF-8?q?=E6=8D=A2=200377.=E7=BB=84=E5=90=88=E6=80=BB=E5=92=8CIV=200518.?= =?UTF-8?q?=E9=9B=B6=E9=92=B1=E5=85=91=E6=8D=A2II=20=E5=A4=9A=E9=87=8D?= =?UTF-8?q?=E8=83=8C=E5=8C=85=E7=90=86=E8=AE=BA=E5=9F=BA=E7=A1=80=20?= =?UTF-8?q?=E8=83=8C=E5=8C=85=E6=80=BB=E7=BB=93=E7=AF=87=20=E6=8E=92?= =?UTF-8?q?=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0139.单词拆分.md | 23 ++++++++------- problems/0279.完全平方数.md | 21 +++++++------- problems/0322.零钱兑换.md | 20 +++++++------ problems/0377.组合总和Ⅳ.md | 20 +++++++------ problems/0518.零钱兑换II.md | 22 +++++++------- problems/背包总结篇.md | 1 + .../背包问题理论基础多重背包.md | 10 +++---- .../背包问题理论基础完全背包.md | 29 ++++++++++--------- 8 files changed, 80 insertions(+), 66 deletions(-) diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md index 0d88ba36..d93288ae 100644 --- a/problems/0139.单词拆分.md +++ b/problems/0139.单词拆分.md @@ -33,9 +33,9 @@ * 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] * 输出: false -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -123,7 +123,7 @@ public: **这个代码就可以AC了,当然回溯算法不是本题的主菜,背包才是!** -## 背包问题 +### 背包问题 单词就是物品,字符串s就是背包,单词能否组成字符串s,就是问物品能不能把背包装满。 @@ -239,7 +239,7 @@ public: } }; -``` +``` 使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下: @@ -259,8 +259,8 @@ public: ## 其他语言版本 +### Java: -Java: ```java class Solution { public boolean wordBreak(String s, List wordDict) { @@ -335,7 +335,7 @@ class Solution { } ``` -Python: +### Python: 回溯 ```python @@ -397,7 +397,8 @@ class Solution: -Go: +### Go: + ```Go func wordBreak(s string,wordDict []string) bool { wordDictSet := make(map[string]bool) @@ -433,7 +434,8 @@ func wordBreak(s string, wordDict []string) bool { } ``` -Javascript: +### JavaScript: + ```javascript const wordBreak = (s, wordDict) => { @@ -454,7 +456,7 @@ const wordBreak = (s, wordDict) => { } ``` -TypeScript: +### TypeScript: > 动态规划 @@ -496,7 +498,7 @@ function wordBreak(s: string, wordDict: string[]): boolean { }; ``` -Rust: +### Rust: ```rust impl Solution { @@ -519,3 +521,4 @@ impl Solution { + diff --git a/problems/0279.完全平方数.md b/problems/0279.完全平方数.md index 70ab8649..a0c138ee 100644 --- a/problems/0279.完全平方数.md +++ b/problems/0279.完全平方数.md @@ -28,9 +28,9 @@ 提示: * 1 <= n <= 10^4 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[换汤不换药!| LeetCode:279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[换汤不换药!| LeetCode:279.完全平方数](https://www.bilibili.com/video/BV12P411T7Br/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -106,8 +106,6 @@ dp[5] = min(dp[4] + 1, dp[1] + 1) = 2 最后的dp[n]为最终结果。 -## C++代码 - 以上动规五部曲分析完毕C++代码如下: ```CPP @@ -165,8 +163,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { // 版本一,先遍历物品, 再遍历背包 @@ -219,7 +217,7 @@ class Solution { } ``` -Python: +### Python: 先遍历物品, 再遍历背包 ```python @@ -276,7 +274,8 @@ class Solution: ``` -Go: +### Go: + ```go // 版本一,先遍历物品, 再遍历背包 func numSquares1(n int) int { @@ -327,7 +326,8 @@ func min(a, b int) int { } ``` -Javascript: +### Javascript: + ```Javascript // 先遍历物品,再遍历背包 var numSquares1 = function(n) { @@ -357,7 +357,7 @@ var numSquares2 = function(n) { }; ``` -TypeScript: +### TypeScript: ```typescript // 先遍历物品 @@ -389,7 +389,7 @@ function numSquares(n: number): number { }; ``` -Rust: +### Rust: ```rust // 先遍历背包 @@ -439,3 +439,4 @@ impl Solution { + diff --git a/problems/0322.零钱兑换.md b/problems/0322.零钱兑换.md index 1f3f4df2..f32fd13e 100644 --- a/problems/0322.零钱兑换.md +++ b/problems/0322.零钱兑换.md @@ -39,9 +39,9 @@ * 1 <= coins[i] <= 2^31 - 1 * 0 <= amount <= 10^4 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包最少的物品件数是多少?| LeetCode:322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包最少的物品件数是多少?| LeetCode:322.零钱兑换](https://www.bilibili.com/video/BV14K411R7yv/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 @@ -110,7 +110,6 @@ dp[0] = 0; dp[amount]为最终结果。 -## C++代码 以上分析完毕,C++ 代码如下: ```CPP @@ -187,8 +186,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int coinChange(int[] coins, int amount) { @@ -215,7 +214,7 @@ class Solution { } ``` -Python: +### Python: 先遍历物品 后遍历背包 @@ -288,7 +287,8 @@ class Solution: ``` -Go: +### Go: + ```go // 版本一, 先遍历物品,再遍历背包 func coinChange1(coins []int, amount int) int { @@ -352,7 +352,7 @@ func min(a, b int) int { ``` -Rust: +### Rust: ```rust // 遍历物品 @@ -398,7 +398,8 @@ impl Solution { } ``` -Javascript: +### Javascript: + ```javascript // 遍历物品 const coinChange = (coins, amount) => { @@ -435,7 +436,7 @@ var coinChange = function(coins, amount) { } ``` -TypeScript: +### TypeScript: ```typescript // 遍历物品 @@ -473,3 +474,4 @@ function coinChange(coins: number[], amount: number): number { + diff --git a/problems/0377.组合总和Ⅳ.md b/problems/0377.组合总和Ⅳ.md index bd43d526..d9699c54 100644 --- a/problems/0377.组合总和Ⅳ.md +++ b/problems/0377.组合总和Ⅳ.md @@ -31,9 +31,9 @@ 因此输出为 7。 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包有几种方法?求排列数?| LeetCode:377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有几种方法?求排列数?| LeetCode:377.组合总和IV](https://www.bilibili.com/video/BV1V14y1n7B6/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 ## 思路 @@ -154,8 +154,7 @@ C++测试用例有两个数相加超过int的数据,所以需要在if里加上 ## 其他语言版本 - -Java: +### Java: ```Java class Solution { @@ -174,7 +173,7 @@ class Solution { } ``` -Python: +### Python: 卡哥版 @@ -207,7 +206,8 @@ class Solution: ``` -Go: +### Go: + ```go func combinationSum4(nums []int, target int) int { //定义dp数组 @@ -226,7 +226,8 @@ func combinationSum4(nums []int, target int) int { } ``` -Javascript: +### Javascript: + ```javascript const combinationSum4 = (nums, target) => { @@ -245,7 +246,7 @@ const combinationSum4 = (nums, target) => { }; ``` -TypeScript: +### TypeScript: ```typescript function combinationSum4(nums: number[], target: number): number { @@ -264,7 +265,7 @@ function combinationSum4(nums: number[], target: number): number { }; ``` -Rust +### Rust: ```Rust impl Solution { @@ -289,3 +290,4 @@ impl Solution { + diff --git a/problems/0518.零钱兑换II.md b/problems/0518.零钱兑换II.md index 8da35114..7c9f0fce 100644 --- a/problems/0518.零钱兑换II.md +++ b/problems/0518.零钱兑换II.md @@ -41,9 +41,9 @@ * 硬币种类不超过 500 种 * 结果符合 32 位符号整数 -# 算法公开课 +## 算法公开课 -**《代码随想录》算法视频公开课:[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[装满背包有多少种方法?组合与排列有讲究!| LeetCode:518.零钱兑换II](https://www.bilibili.com/video/BV1KM411k75j/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 @@ -202,8 +202,8 @@ public: ## 其他语言版本 +### Java: -Java: ```Java class Solution { public int change(int amount, int[] coins) { @@ -242,7 +242,7 @@ class Solution { } ``` -Python: +### Python: ```python @@ -260,7 +260,8 @@ class Solution: -Go: +### Go: + ```go func change(amount int, coins []int) int { // 定义dp数组 @@ -280,7 +281,8 @@ func change(amount int, coins []int) int { } ``` -Rust: +### Rust: + ```rust impl Solution { pub fn change(amount: i32, coins: Vec) -> i32 { @@ -297,7 +299,8 @@ impl Solution { } ``` -Javascript: +### Javascript: + ```javascript const change = (amount, coins) => { let dp = Array(amount + 1).fill(0); @@ -313,7 +316,7 @@ const change = (amount, coins) => { } ``` -TypeScript: +### TypeScript: ```typescript function change(amount: number, coins: number[]): number { @@ -328,7 +331,7 @@ function change(amount: number, coins: number[]): number { }; ``` -Scala: +### Scala: ```scala object Solution { @@ -349,4 +352,3 @@ object Solution { - diff --git a/problems/背包总结篇.md b/problems/背包总结篇.md index c4e8cd9c..9be93096 100644 --- a/problems/背包总结篇.md +++ b/problems/背包总结篇.md @@ -106,3 +106,4 @@ + diff --git a/problems/背包问题理论基础多重背包.md b/problems/背包问题理论基础多重背包.md index 1a856bf5..50c2e5bf 100644 --- a/problems/背包问题理论基础多重背包.md +++ b/problems/背包问题理论基础多重背包.md @@ -144,8 +144,7 @@ int main() { ## 其他语言版本 - -Java: +### Java: ```Java public void testMultiPack1(){ @@ -192,7 +191,7 @@ public void testMultiPack2(){ } ``` -Python: +### Python: 改变物品数量为01背包格式(无参版) ```python @@ -315,7 +314,7 @@ if __name__ == "__main__": test_multi_pack(weight, value, nums, bagWeight) ``` -Go: +### Go: ```go package theory @@ -406,7 +405,7 @@ func Test_multiplePack(t *testing.T) { PASS ``` -TypeScript: +### TypeScript: > 版本一(改变数据源): @@ -469,3 +468,4 @@ testMultiPack(); + diff --git a/problems/背包问题理论基础完全背包.md b/problems/背包问题理论基础完全背包.md index 088a3d50..ac4c4a1c 100644 --- a/problems/背包问题理论基础完全背包.md +++ b/problems/背包问题理论基础完全背包.md @@ -7,9 +7,13 @@ # 动态规划:完全背包理论基础 -**《代码随想录》算法视频公开课:[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 +## 算法公开课 -## 完全背包 +**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[带你学透完全背包问题! ](https://www.bilibili.com/video/BV1uK411o7c9/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。 + +## 思路 + +### 完全背包 有N件物品和一个最多能背重量为W的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。**每件物品都有无限个(也就是可以放入背包多次)**,求解将哪些物品装入背包里物品价值总和最大。 @@ -113,8 +117,6 @@ for(int j = 0; j <= bagWeight; j++) { // 遍历背包容量 } ``` -## C++测试代码 - 完整的C++测试代码如下: ```CPP @@ -181,7 +183,7 @@ int main() { ## 其他语言版本 -Java: +### Java: ```java //先遍历物品,再遍历背包 @@ -221,9 +223,7 @@ private static void testCompletePackAnotherWay(){ -Python: - - +### Python: 先遍历物品,再遍历背包(无参版) ```python @@ -299,7 +299,8 @@ if __name__ == "__main__": ``` -Go: +### Go: + ```go // test_CompletePack1 先遍历物品, 在遍历背包 @@ -352,7 +353,8 @@ func main() { fmt.Println(test_CompletePack2(weight, price, 4)) } ``` -Javascript: +### Javascript: + ```Javascript // 先遍历物品,再遍历背包容量 function test_completePack1() { @@ -385,7 +387,7 @@ function test_completePack2() { } ``` -TypeScript: +### TypeScript: ```typescript // 先遍历物品,再遍历背包容量 @@ -404,7 +406,7 @@ function test_CompletePack(): void { test_CompletePack(); ``` -Scala: +### Scala: ```scala // 先遍历物品,再遍历背包容量 @@ -426,7 +428,7 @@ object Solution { } ``` -Rust: +### Rust: ```rust impl Solution { @@ -468,3 +470,4 @@ fn test_complete_pack() { +